简体   繁体   English

Discord.py 机器人只回复一条消息

[英]Discord.py bot only replies to one message

so im making 2 discord bots: one will listen for commands and when it recieves one it will send a message in a private channel.所以我正在制作 2 个 discord 机器人:一个会监听命令,当它收到一个时,它将在私人频道中发送消息。 when the first bot sends a message in that channel the second one will respond to indicate that it's online, then bot 1 requests to start a Minecraft server.当第一个机器人在该频道中发送消息时,第二个机器人将响应以指示它在线,然后机器人 1 请求启动 Minecraft 服务器。 the problem is, bot 2 only responds once and if I want it to respond more than once I need to manually restart the code.问题是,bot 2 只响应一次,如果我希望它响应不止一次,我需要手动重新启动代码。 im on python 3.8.6我在 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')

what's the problem and how can i fix it?有什么问题,我该如何解决?

I believe (but cannot test, as I do not have an discord bot to test on) that you cannot redefine on_message inside itself and update the client on_message handler.我相信(但无法测试,因为我没有要测试的 discord 机器人)您无法在其内部重新定义 on_message 并更新客户端 on_message 处理程序。 You can try the following instead, which does not declare a new on_message function:您可以尝试以下方法,它不会声明新的 on_message function:

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')

You need to put await client.process_commands(message) in the end你需要把await client.process_commands(message)放在最后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM