【问题标题】:Discord.py bot only replies to one messageDiscord.py 机器人只回复一条消息
【发布时间】:2023-03-21 12:15:01
【问题描述】:

所以我制作了 2 个不和谐机器人:一个会监听命令,当它收到一个时,它将在私人频道中发送消息。当第一个机器人在该频道中发送消息时,第二个机器人将响应以指示它在线,然后机器人 1 请求启动 Minecraft 服务器。问题是,bot 2 只响应一次,如果我希望它响应不止一次,我需要手动重新启动代码。我在 python 3.8.6 上

import discord

client = discord.Client()

@client.event
async def on_message(message):
    if message.author == client.user:
        return
        
    if message.author.id == bot 1 id:
        if message.content.startswith('<@!bot 2 id>'):
            starterChannel = client.get_channel('private channel')
            channel = message.channel
            if channel == starterChannel:
                await channel.send('ye')
                @client.event
                async def on_message(message):
                    if message.content.startswith('Can'):
                        message = message.content
                        request = message[8:]
                        print(request)
                        if request.startswith('start'):
                            await channel.send('idk how')

有什么问题,我该如何解决?

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    我相信(但无法测试,因为我没有要测试的不和谐机器人)您无法在其内部重新定义 on_message 并更新客户端 on_message 处理程序。您可以尝试以下方法,它不会声明新的 on_message 函数:

    import discord
    
    client = discord.Client()
    
    @client.event
    async def on_message(message):
        if message.author == client.user:
            return
            
        if message.author.id == bot 1 id:
            if message.content.startswith('<@!bot 2 id>'):
                starterChannel = client.get_channel('private channel')
                channel = message.channel
                if channel == starterChannel:
                    await channel.send('ye')
    
            if message.content.startswith('Can'):
                channel = message.channel
                message = message.content
                request = message[8:]
                print(request)
                if request.startswith('start'):
                    await channel.send('idk how')
    

    【讨论】:

    • 我创建了这个帐户是为了问这个问题,所以它不会显示我的赞成票,但这有效,谢谢!
    【解决方案2】:

    你需要把await client.process_commands(message)放在最后

    【讨论】:

    • 谢谢,但 thshea 的回答对我有用
    • @linkthehero1234 请将 thshea 的回答标记为已接受
    猜你喜欢
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-05
    相关资源
    最近更新 更多