我想在命令函数下使用 return ,我作为变量是什么?
I want to use return under a command function, what do I as the variable?
我想阻止我的机器人自己回答,所以我尝试设置一个 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)
@client.event
async def on_message(message):
if message.author == client.user:
return
你已经做对了。 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 和 message 拼错了!命令没有消息变量!
@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)
所以基本上你只需要将代码更改为:
@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)
希望对您有所帮助!
到目前为止,我只看到了告诉您需要做什么的答案。但不是为什么你需要按照他们告诉你的去做。这个答案会给你一个策略和解决方案,这样你就不需要再问这些问题了。 (不是说错了,而是自己很容易发现,这样可以节省很多时间)。
第 1 步:在文档中搜索事件(或在本例中如何创建命令)
因为我们想了解如何发出命令。我们首先搜索如何发出命令。例如 this link helps you make commands. Getting to this link can be done by typing in "How to make commands in discord py", and taking the first link that is from the official documentation: https://discordpy.readthedocs.io/.
第 2 步:查看您从事件中获得了哪些参数(对象)
当我们阅读文档时,您会在第一部分看到它使用 context object. And that you can use it through the input from the function (The variable ctx). We now want to investigate what we can do with the ctx/context object。
第 3 步:了解对象的用途
如前所述,我们想了解如何使用这些对象。在这种情况下,我们专注于“ctx”对象。描述上下文对象的页面告诉了 ctx 对象包含的所有内容。如您所见,出现的第一个参数是 message object. Which was part of what you wanted. Thus accessing it would simply have been: ctx.message.author
. But how do other people say use: ctx.author
? Well if you read the context page further we also see an Author 成员。它还指出它是 Message.Author 的 shorthand。这意味着我们还可以使用:ctx.author
,以获得完全相同的结果。
第 4 步:在您的代码中集成对象
现在我们知道上下文对象是什么,以及我们可以用它做什么。但是我们如何整合它呢?
首先,提醒我们我们想要实现的目标是很有用的。我们想阻止机器人响应自己。因此,只需忽略可能来自您的机器人的消息。我们需要比较传入消息的作者是否是机器人,如果它来自机器人我们需要忽略它。
如前面第 3 步所述。我们可以使用 ctx.message.author
或 ctx.author
来获取作者。如果这等于机器人 (client.user
) 那么我们应该 return 早点。
这会导致检查两个值的 if 语句:
@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.
_________________________________________________________________
我建议阅读文档。因为它有很大帮助。通常,您从 events/commands 获得的输入就是完成工作所需的全部内容。如果您确切知道自己需要什么,它也很方便。例如,您可以搜索从函数获得的所有对象,并搜索特定变量和描述,在这种情况下您需要“作者”。这样可以加快搜索速度。
无论如何,了解常用对象的用途是很有用的。因为这有助于为您的机器人编写代码。
我想阻止我的机器人自己回答,所以我尝试设置一个 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)
@client.event
async def on_message(message):
if message.author == client.user:
return
你已经做对了。 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 和 message 拼错了!命令没有消息变量!
@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)
所以基本上你只需要将代码更改为:
@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)
希望对您有所帮助!
到目前为止,我只看到了告诉您需要做什么的答案。但不是为什么你需要按照他们告诉你的去做。这个答案会给你一个策略和解决方案,这样你就不需要再问这些问题了。 (不是说错了,而是自己很容易发现,这样可以节省很多时间)。
第 1 步:在文档中搜索事件(或在本例中如何创建命令)
因为我们想了解如何发出命令。我们首先搜索如何发出命令。例如 this link helps you make commands. Getting to this link can be done by typing in "How to make commands in discord py", and taking the first link that is from the official documentation: https://discordpy.readthedocs.io/.
第 2 步:查看您从事件中获得了哪些参数(对象)
当我们阅读文档时,您会在第一部分看到它使用 context object. And that you can use it through the input from the function (The variable ctx). We now want to investigate what we can do with the ctx/context object。
第 3 步:了解对象的用途
如前所述,我们想了解如何使用这些对象。在这种情况下,我们专注于“ctx”对象。描述上下文对象的页面告诉了 ctx 对象包含的所有内容。如您所见,出现的第一个参数是 message object. Which was part of what you wanted. Thus accessing it would simply have been: ctx.message.author
. But how do other people say use: ctx.author
? Well if you read the context page further we also see an Author 成员。它还指出它是 Message.Author 的 shorthand。这意味着我们还可以使用:ctx.author
,以获得完全相同的结果。
第 4 步:在您的代码中集成对象
现在我们知道上下文对象是什么,以及我们可以用它做什么。但是我们如何整合它呢?
首先,提醒我们我们想要实现的目标是很有用的。我们想阻止机器人响应自己。因此,只需忽略可能来自您的机器人的消息。我们需要比较传入消息的作者是否是机器人,如果它来自机器人我们需要忽略它。
如前面第 3 步所述。我们可以使用 ctx.message.author
或 ctx.author
来获取作者。如果这等于机器人 (client.user
) 那么我们应该 return 早点。
这会导致检查两个值的 if 语句:
@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.
_________________________________________________________________
我建议阅读文档。因为它有很大帮助。通常,您从 events/commands 获得的输入就是完成工作所需的全部内容。如果您确切知道自己需要什么,它也很方便。例如,您可以搜索从函数获得的所有对象,并搜索特定变量和描述,在这种情况下您需要“作者”。这样可以加快搜索速度。
无论如何,了解常用对象的用途是很有用的。因为这有助于为您的机器人编写代码。