在我添加新的代码块之后,discord.py机器人停止响应

时间:2020-10-03 19:53:12

标签: python discord discord.py

是python和discord.py的新功能,这是具有更新的详细信息且没有难以理解的代码块的重新发布。我尚未在线找到此问题的答案,因此这可能是我的一个小错误。

问题 我正在尝试为服务器编程一个discord.py机器人,它可以执行一些基本命令。 我想在外壳中添加一些功能,使我可以通过python shell控制机器人。 在此之前,我有一些基本命令可以按预期工作(我执行c!help,并且它会在嵌入消息的帮助下进行响应) 通过控制台添加用于控制的代码后,discord命令停止了按预期的响应。

期望的行为:我在不和谐中键入c!boop {user},机器人将dm发送给该用户,并且在日志记录通道中发送了一条日志消息。我在python shell中执行c!console,出现交互式菜单

发生了什么:我不和谐地输入c!boop {user},我什么也没回来。我在python shell中执行c!console,出现了交互式菜单。

正如我所说,在为Shell添加新代码之前,它就起作用了。

我的代码 对于MRE,这里的代码已经大大缩短了,但是如果您认为完整的代码是必要的,请询问。抱歉,如果它仍然很长,我已经删除了3/4,只保留与我的问题相关的部分。

import discord
import time

client = discord.Client()
prefix = 'c!'

@client.event
async def on_message(message):
    if message.author == client.user:
        return
#This bit is the command for within discord
    if message.content.startswith(prefix + "boop"):
        
        #Getting the Victim's user id
        victim = str(message.content)
        victim = victim.replace(prefix + 'boop ', '')
        victim = victim.replace('<@', '')
        victim = victim.replace('!', '')
        victim = victim.replace('>','')
        
        #Booping the user
        user = client.get_user(int(victim))
        await message.channel.send("Booped " + user.name)
        await user.send('**Boop!**')
        t = time.localtime()
        current_time = time.strftime("%H:%M:%S", t)
        channel = client.get_channel(int(759798825161326593))
        LogMsg = str('`' + current_time + '` ' + message.author.name + ' used command in ' + str(message.channel) + ' `' + message.content + '`')
        await channel.send(LogMsg)


#After I added this section, the above command stopped working
@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print('USER ID: ' + str(client.user.id))
    print('')
    print('To Open the Console, type ' + prefix + 'console in the shell')
    print('------')

    console = str(prefix + 'console')
    while 1 == 1:
        ConsoleInput = input('')
        if ConsoleInput == console:
            while 1 == 1:
                print('------')
                print('Please Select a Module')
                print('1 - Announce')
                print('99 - Exit Console')
                print('------')
                ConsoleInput = int(input(''))

                if ConsoleInput == 1:
                    print('------')
                    print('Module 1 Selected - Announce')
                    print("What's the id of the channel you want to announce in?")
                    Channel_id = int(input())
                    print("Embed? (1 for yes, 2 for no)")
                    YeNo = int(input())
                    
                    if YeNo == 1:
                        print("What is the Title for the Embed message?")
                        EmbedTitle = str(input())
                        print("What is the Description for the Embed message?")
                        announcement = str(input())
                        print('Announcing')
                        channel = client.get_channel(Channel_id)
                        embed=discord.Embed(title=EmbedTitle, description=announcement, color=0xff40ff)
                        await channel.send(embed=embed)
                        print("Announced")

                        t = time.localtime()
                        current_time = time.strftime("%H:%M:%S", t)
                        channel = client.get_channel(int(759798825161326593))
                        await channel.send('`' + current_time + '` ' + 'Console User used command in Console ' '`' + str(Channel_id) + ' ' + EmbedTitle + ' ' + announcement + ' ' + str(YeNo) + '`')
                        
                    elif YeNo == 2:
                        print("What is the announcement?")
                        announcement = str(input())
                        print("Announcing")
                        channel = client.get_channel(Channel_id)
                        await channel.send(announcement)
                        print("Announced")
                        
                        t = time.localtime()
                        current_time = time.strftime("%H:%M:%S", t)
                        channel = client.get_channel(int(759798825161326593))
                        await channel.send('`' + current_time + '` ' + 'Console User used command in Console ' '`' + str(Channel_id) + ' ' + announcement + ' ' + str(YeNo) + '`')

                elif ConsoleInput == 99:
                    print('------')
                    print('Exiting Console')
                    print('You can restart the console by typing ' + prefix + 'console in the shell')
                    print('------')
                    break

client.run(TOKEN GOES HERE)

预先感谢

1 个答案:

答案 0 :(得分:0)

阻止代码

on_ready事件中的同步代码似乎是使机器人停止运行的原因;等待来自控制台的输入会停止运行其他任何代码,包括对命令做出反应。

解决此问题的唯一方法是以不涉及使用控制台的另一种方式设计公告命令,例如将其作为机器人的命令。

关于discord.Client()的注释

由于您使用的是较低级别的API(discord.Client),因此可能会发现为机器人开发新命令更加困难。我建议使用discord.py随附的bot commands frameworkdiscord.ext.commands)。间距位于链接中,因此,下面是使用带有boop命令的框架的示例:

import time
import discord
from discord.ext import commands

prefix = 'c!'
TOKEN = ''

client = commands.Bot(command_prefix=prefix)

@client.event
async def on_ready():
    print('Logged in as', client.user.name)

@client.command(name='boop')
async def client_boop(ctx, user: discord.User):
    """Boops a user.
Accepted inputs are: ID, mention, name#discrim, or name
Example: c!boop thegamecracks"""
    await user.send('**Boop!**')
    await ctx.channel.send("Booped " + user.name)

    current_time = time.strftime("%H:%M:%S", time.localtime())
    log_channel = client.get_channel(759798825161326593)
    LogMsg = '`{}` {} used command in {} `{}`'.format(
        current_time,
        ctx.author.name,
        ctx.channel.name,
        ctx.message.content
    )
    await log_channel.send(LogMsg)

client.run(TOKEN)