Discord.py Bot 执行命令两次
我的机器人执行这些命令,其中包括 await bot.process_commands(ctx)
& 等待 bot.process_commands(消息)
。我该如何修复?
#playing a game status
@bot.command()
@commands.has_permissions(manage_guild=True)
async def g_status(ctx, *, status):
embed = discord.Embed(description =f"✅ Status changed to Playing a game `{status}`", color= 0x303136)
await bot.change_presence(activity=discord.Game(name=f"{status}"))
await ctx.send(embed=embed)
await bot.process_commands(ctx)
#watching status
@bot.command()
@commands.has_permissions(manage_guild=True)
async def w_status(ctx, *, status):
embed = discord.Embed(description =f"✅ Status changed to Watching `{status}`", color= 0x303136)
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=f"{status}"))
await ctx.send(embed=embed)
await bot.process_commands(ctx)
#listen status
@bot.command()
@commands.has_permissions(manage_guild=True)
async def l_status(ctx, *, status):
embed = discord.Embed(description =f"✅ Status changed to Listening to `{status}`", color= 0x303136)
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name=f"{status}"))
await ctx.send(embed=embed)
await bot.process_commands(ctx)
#clear status
@bot.command()
@commands.has_permissions(manage_guild=True)
async def clear_status(c):
await bot.change_presence(status=None)
embed = discord.Embed(description =f"✅ Status cleared", color= 0x303136)
await c.send(embed=embed)
await bot.process_commands(c)
#boost
@bot.event
async def on_message(message):
if "MessageType.premium_guild" in str(message.type):
if message.guild.id == 928443083660607549:
if "|" in message.author.display_name:
name0 = message.author.display.split("|")
display = name0[0]
else:
display = message.author.display_name
embed = discord.Embed(title=f"thanks for boosting, {display}!", description="thanks for boosting ikari <3", color=0x303136)
await message.channel.send(f"{message.author.mention}")
await message.channel.send(embed=embed)
else:
return
await bot.process_commands(message)
#send msgs via dms
@bot.command()
@commands.has_permissions(manage_guild = True)
async def dm(ctx, member: discord.User):
await ctx.send('Message?')
def check(m):
return m.author.id == ctx.author.id
message = await bot.wait_for('message', check = check)
embed = discord.Embed(title="**Message sent!**", color=0x303136)
embed.set_author(name=f"sent by: {message.author}", icon_url= message.author.avatar_url)
embed.add_field(name="**Message:**", value=f"{message.content}", inline = False)
msg = discord.Embed(description=f'**{message.content}**', color=0x303136)
await ctx.send(embed=embed)
await member.send(embed=msg)
await bot.process_commands(message)
#typing
@bot.command()
async def helloworld(ctx):
async with ctx.typing():
await asyncio.sleep(0.5)
await ctx.send('Hello World!')
#recieve dm test
@bot.event
async def on_message(message: discord.Message):
if message.guild is None and not message.author.bot:
print(message.content)
await bot.process_commands(message)
msg_dump_channel = 932252699657908225
@bot.event
async def on_message(message: discord.Message):
channel = bot.get_channel(msg_dump_channel)
if message.guild is None and not message.author.bot:
embed = discord.Embed(description=f"**{message.content}**\n sent by{message.author.mention}", color=0x303136)
embed.set_author(name=f"{message.author}", icon_url= message.author.avatar_url)
await channel.send(embed=embed)
await bot.process_commands(message)
#send text
@bot.command()
@commands.has_permissions(manage_guild = True)
async def send(ctx, *, channel: discord.TextChannel):
msg_chan = channel
await ctx.send('?')
def check(m):
return m.author.id == ctx.author.id
message = await bot.wait_for('message', check = check)
embed = discord.Embed(title="✅ **Message sent!**", color=0x303136)
embed.set_author(name=f"sent by: {message.author}", icon_url= message.author.avatar_url)
embed.add_field(name="**Message:**", value=f"{message.content}", inline = False)
await ctx.send(embed=embed)
async with msg_chan.typing():
await asyncio.sleep(0.5)
await msg_chan.send(f'{message.content}')
await bot.process_commands(message)
#rep give role
@bot.listen('on_message')
async def on_message(message):
if message.channel.id == 928443084205867038:
role_name = "rep 1"
lvl5 = "lvl 5"
if "rep 1" in message.content:
role = discord.utils.get(message.guild.roles, name=role_name)
await message.author.add_roles(role)
await message.add_reaction("<:true:928510447169196082>")
if "rep 2" in message.content:
rep1 = discord.utils.get(message.guild.roles, name=role_name)
if rep1 in message.author.roles:
rep1 = discord.utils.get(message.guild.roles, name=role_name)
rep = discord.utils.get(message.guild.roles, name=lvl5)
await message.author.add_roles(rep)
await message.author.remove_roles(rep1)
await message.add_reaction("<:true:928510447169196082>")
else:
await message.channel.send('You must rep once')
await bot.process_commands(message)
My bot executes these commands that include await bot.process_commands(ctx)
& await bot.process_commands(message)
. How may I fix?
#playing a game status
@bot.command()
@commands.has_permissions(manage_guild=True)
async def g_status(ctx, *, status):
embed = discord.Embed(description =f"✅ Status changed to Playing a game `{status}`", color= 0x303136)
await bot.change_presence(activity=discord.Game(name=f"{status}"))
await ctx.send(embed=embed)
await bot.process_commands(ctx)
#watching status
@bot.command()
@commands.has_permissions(manage_guild=True)
async def w_status(ctx, *, status):
embed = discord.Embed(description =f"✅ Status changed to Watching `{status}`", color= 0x303136)
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=f"{status}"))
await ctx.send(embed=embed)
await bot.process_commands(ctx)
#listen status
@bot.command()
@commands.has_permissions(manage_guild=True)
async def l_status(ctx, *, status):
embed = discord.Embed(description =f"✅ Status changed to Listening to `{status}`", color= 0x303136)
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name=f"{status}"))
await ctx.send(embed=embed)
await bot.process_commands(ctx)
#clear status
@bot.command()
@commands.has_permissions(manage_guild=True)
async def clear_status(c):
await bot.change_presence(status=None)
embed = discord.Embed(description =f"✅ Status cleared", color= 0x303136)
await c.send(embed=embed)
await bot.process_commands(c)
#boost
@bot.event
async def on_message(message):
if "MessageType.premium_guild" in str(message.type):
if message.guild.id == 928443083660607549:
if "|" in message.author.display_name:
name0 = message.author.display.split("|")
display = name0[0]
else:
display = message.author.display_name
embed = discord.Embed(title=f"thanks for boosting, {display}!", description="thanks for boosting ikari <3", color=0x303136)
await message.channel.send(f"{message.author.mention}")
await message.channel.send(embed=embed)
else:
return
await bot.process_commands(message)
#send msgs via dms
@bot.command()
@commands.has_permissions(manage_guild = True)
async def dm(ctx, member: discord.User):
await ctx.send('Message?')
def check(m):
return m.author.id == ctx.author.id
message = await bot.wait_for('message', check = check)
embed = discord.Embed(title="**Message sent!**", color=0x303136)
embed.set_author(name=f"sent by: {message.author}", icon_url= message.author.avatar_url)
embed.add_field(name="**Message:**", value=f"{message.content}", inline = False)
msg = discord.Embed(description=f'**{message.content}**', color=0x303136)
await ctx.send(embed=embed)
await member.send(embed=msg)
await bot.process_commands(message)
#typing
@bot.command()
async def helloworld(ctx):
async with ctx.typing():
await asyncio.sleep(0.5)
await ctx.send('Hello World!')
#recieve dm test
@bot.event
async def on_message(message: discord.Message):
if message.guild is None and not message.author.bot:
print(message.content)
await bot.process_commands(message)
msg_dump_channel = 932252699657908225
@bot.event
async def on_message(message: discord.Message):
channel = bot.get_channel(msg_dump_channel)
if message.guild is None and not message.author.bot:
embed = discord.Embed(description=f"**{message.content}**\n sent by{message.author.mention}", color=0x303136)
embed.set_author(name=f"{message.author}", icon_url= message.author.avatar_url)
await channel.send(embed=embed)
await bot.process_commands(message)
#send text
@bot.command()
@commands.has_permissions(manage_guild = True)
async def send(ctx, *, channel: discord.TextChannel):
msg_chan = channel
await ctx.send('?')
def check(m):
return m.author.id == ctx.author.id
message = await bot.wait_for('message', check = check)
embed = discord.Embed(title="✅ **Message sent!**", color=0x303136)
embed.set_author(name=f"sent by: {message.author}", icon_url= message.author.avatar_url)
embed.add_field(name="**Message:**", value=f"{message.content}", inline = False)
await ctx.send(embed=embed)
async with msg_chan.typing():
await asyncio.sleep(0.5)
await msg_chan.send(f'{message.content}')
await bot.process_commands(message)
#rep give role
@bot.listen('on_message')
async def on_message(message):
if message.channel.id == 928443084205867038:
role_name = "rep 1"
lvl5 = "lvl 5"
if "rep 1" in message.content:
role = discord.utils.get(message.guild.roles, name=role_name)
await message.author.add_roles(role)
await message.add_reaction("<:true:928510447169196082>")
if "rep 2" in message.content:
rep1 = discord.utils.get(message.guild.roles, name=role_name)
if rep1 in message.author.roles:
rep1 = discord.utils.get(message.guild.roles, name=role_name)
rep = discord.utils.get(message.guild.roles, name=lvl5)
await message.author.add_roles(rep)
await message.author.remove_roles(rep1)
await message.add_reaction("<:true:928510447169196082>")
else:
await message.channel.send('You must rep once')
await bot.process_commands(message)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
on_message
将在您在 Discord 频道或 DM 聊天中收到消息时执行(链接)。所以你只需要一个on_message
函数我不知道为什么你的代码有多个
on_message
所以有一些方法可以修复它。on_message
中,如果您需要创建另一个要使用
on_message
执行的函数,那么您需要这样做。on_message
will be execute when you have a message in discord channel or DM chat(link). So you need just oneon_message
functionI don't know why your code have multi
on_message
so there are some way to fix it.on_message
If you need to make another function that want to execute with
on_message
so you need to do this.您遇到的问题是
bot.process_commands
。它的作用是处理来自消息
的命令
。在内部,它在默认的on_message
中使用来使命令
运行。当你仔细想想,这是有道理的。但是,当您覆盖默认的on_message
时,它会覆盖bot.process_commands
,因此您的命令将停止工作。(实际上,Discord.py 的官方常见问题解答涵盖了这一点:为什么 on_message 会让我的命令停止工作?)
为了让我们的命令再次工作,我们仅添加一个
bot.process_commands
到on_message
的末尾。但在您的代码中,您运行了两个on_message
事件和两个bot.process_command
,因此我们处理命令两次。这就是为什么你的命令会起作用两次。只需从自定义on_message
事件中删除一个事件或根本不使用它即可。The problem you are having is
bot.process_commands
. What that does is, well, it processescommands
from amessage
. And internally it is used in the defaulton_message
to makecommands
going. When you think about it, it makes sense. But, when you override the defaulton_message
it overrides thatbot.process_commands
so your commands stops working.(Actually, Discord.py's official F.A.Q covered this: Why does on_message make my commands stop working?)
To make our commands work again, we are adding only one
bot.process_commands
to end of ouron_message
. But in your code, you have twoon_message
events running and twobot.process_command
s so we are processing our commands twice. This is why your commands works twice. Just delete one from your customon_message
event or not use it at all.