Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我想在命令函数下使用return,我该怎么做作为变量?_Python_Bots_Discord_Discord.py_Discord.py Rewrite - Fatal编程技术网

Python 我想在命令函数下使用return,我该怎么做作为变量?

Python 我想在命令函数下使用return,我该怎么做作为变量?,python,bots,discord,discord.py,discord.py-rewrite,Python,Bots,Discord,Discord.py,Discord.py Rewrite,我想阻止我的机器人自动应答,所以我尝试设置一个if语句,显然它不是正确的。。在这种情况下,我用什么替换message.author @client.command() async def amy(ctx): if message.author == client.user: return else: gif = discord.Embed(color = 0x83B5E3) gif.set_image(url = 'https://

我想阻止我的机器人自动应答,所以我尝试设置一个if语句,显然它不是正确的。。在这种情况下,我用什么替换message.author

@client.command()
async def amy(ctx):
    if message.author == client.user:
        return
    else:
        gif = discord.Embed(color = 0x83B5E3)
        gif.set_image(url = 'https://media.giphy.com/media/2yyKE8ZEgrAXPx5gwI/giphy.gif')
        await ctx.channel.send(embed=gif)
我知道如何处理事件,只是不知道在命令下使用什么

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    else:
        gif = discord.Embed(color = 0x83B5E3)
        gif.set_image(url = 'https://media.giphy.com/media/2yyKE8ZEgrAXPx5gwI/giphy.gif')
        await ctx.channel.send(embed=gif)


你已经做对了。if需要位于函数的顶部。似乎还有其他一些问题。

如果我理解正确,您不希望您的程序响应来自自身的命令。 如果是这种情况,请使用ctx.author而不是message.author

尝试使用以下代码:

@client.command()
async def amy(ctx):
    if ctx.author == client.user:
        return
    else:
        gif = discord.Embed(color = 0x83B5E3)
        gif.set_image(url = 'https://media.giphy.com/media/2yyKE8ZEgrAXPx5gwI/giphy.gif')
        await ctx.channel.send(embed=gif)

哟!!我发现你把ctx和信息拼错了!命令没有消息变量

@client.command()
async def amy(ctx):
    if message.author == client.user: # <-- RIGHT HERE
        return
    else:
        gif = discord.Embed(color = 0x83B5E3)
        gif.set_image(url = 'https://media.giphy.com/media/2yyKE8ZEgrAXPx5gwI/giphy.gif')
        await ctx.channel.send(embed=gif) 

希望有帮助

到目前为止,我只看到了告诉你需要做什么的答案。但这不是你为什么要照他们说的做。这个答案会给你一个策略和解决方案,这样你就不必再问这些问题了。(并不是说这是错的,而是你自己可以很容易地找到它,从而节省了很多时间)

步骤1:在文档中搜索事件(或在本例中如何创建命令)

因为我们想了解我们如何发出命令。我们首先研究如何发出命令。例如,链接帮助您生成命令。可以通过键入“如何在discord py中生成命令”并获取官方文档中的第一个链接来访问此链接:

步骤2:查看从事件中获得的参数(对象)

当我们阅读文档时,您将在第一部分看到它使用了一个对象。您可以通过函数(变量ctx)的输入来使用它。我们现在想调查一下我们能用这个做些什么

第3步:了解对象可用于什么

如前所述,我们希望了解如何使用这些对象。在本例中,我们主要关注“ctx”对象。描述上下文对象的页面告诉ctx对象包含的所有内容。如您所见,出现的第一个参数是。这是你想要的。因此,访问它只需:
ctx.message.author
。但是其他人怎么说使用:
ctx.author
?如果你进一步阅读上下文页面,我们还会看到一个成员。它还声明它是Message.Author的缩写。这意味着我们也可以使用:
ctx.author
,以获得完全相同的结果

步骤4:将对象集成到代码中

现在我们知道了上下文对象是什么,以及我们可以用它做什么。但是我们如何整合它呢

首先,提醒我们想要实现什么是有用的。我们想阻止机器人对自身做出响应。因此,只需忽略可能来自bot的消息。我们需要比较传入消息的作者是否是bot,如果它来自bot,我们需要忽略它

如前步骤3所述。我们可以使用
ctx.message.author
ctx.author
获取作者。如果这等于bot(
client.user
),那么我们应该提前返回

这将产生一个检查两个值的if语句:

@client.command()
async def amy(ctx):
    if ctx.author == client.user:
        return
    else:
        # the rest of your code.

\Uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

我建议阅读文档。因为这很有帮助。通常,从事件/命令中获得的输入就是完成工作所需的全部内容。如果你确切地知道你需要什么,它也很方便。例如,您可以搜索从函数中获得的所有对象,并在其中搜索特定变量和描述,在这种情况下,您需要“Author”。这可以加快搜索速度

无论如何,知道什么是通用对象是有用的。因为这对编写bot代码有很大帮助

@client.command()
async def amy(ctx):
    if ctx.author == client.user:
        return
    else:
        gif = discord.Embed(color = 0x83B5E3)
        gif.set_image(url = 'https://media.giphy.com/media/2yyKE8ZEgrAXPx5gwI/giphy.gif')
        await ctx.channel.send(embed=gif)
@client.command()
async def amy(ctx):
    if ctx.author == client.user:
        return
    else:
        # the rest of your code.
@client.command()
async def amy(ctx):
    if ctx.message.author == client.user:
        return
    else:
        # the rest of your code.