Discord.py Bot 执行命令两次

发布于 2025-01-11 02:06:01 字数 5678 浏览 0 评论 0原文

我的机器人执行这些命令,其中包括 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岛徒 2025-01-18 02:06:01

on_message 将在您在 Discord 频道或 DM 聊天中收到消息时执行(链接)。所以你只需要一个 on_message 函数

我不知道为什么你的代码有多个 on_message 所以有一些方法可以修复它。

  • 将所有相关内容合并到一个on_message中,
@bot.event
async def on_message(message: discord.Message):
    #Do everything related in here
  • 为专用案例使用专用API,您可以阅读以获取更多信息Discord API

如果您需要创建另一个要使用 on_message 执行的函数,那么您需要这样做。

@bot.listen('on_message')
async def my_message(message):
    print('two')

on_message will be execute when you have a message in discord channel or DM chat(link). So you need just one on_message function

I don't know why your code have multi on_message so there are some way to fix it.

  • Combine everything related in one on_message
@bot.event
async def on_message(message: discord.Message):
    #Do everything related in here
  • Use dedicated API for dedicated case, you can read for more information Discord API

If you need to make another function that want to execute with on_message so you need to do this.

@bot.listen('on_message')
async def my_message(message):
    print('two')
2025-01-18 02:06:01

您遇到的问题是bot.process_commands。它的作用是处理来自消息命令。在内部,它在默认的 on_message 中使用来使命令运行。当你仔细想想,这是有道理的。但是,当您覆盖默认的 on_message 时,它会覆盖 bot.process_commands,因此您的命令将停止工作。

(实际上,Discord.py 的官方常见问题解答涵盖了这一点:为什么 on_message 会让我的命令停止工作?

为了让我们的命令再次工作,我们添加一个bot.process_commandson_message 的末尾。但在您的代码中,您运行了两个 on_message 事件和两个 bot.process_command,因此我们处理命令两次。这就是为什么你的命令会起作用两次。只需从自定义 on_message 事件中删除一个事件或根本不使用它即可。

The problem you are having is bot.process_commands. What that does is, well, it processes commands from a message. And internally it is used in the default on_message to make commands going. When you think about it, it makes sense. But, when you override the default on_message it overrides that bot.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 our on_message. But in your code, you have two on_message events running and two bot.process_commands so we are processing our commands twice. This is why your commands works twice. Just delete one from your custom on_message event or not use it at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文