当我添加一些代码时,其余代码停止工作

时间:2020-10-03 16:19:24

标签: python discord discord.py

详细信息: 我正在为服务器编程一个discord.py机器人,我想为python shell添加一些命令,这些命令可以通过控制台发送消息。

问题: 当我添加此代码时,它可以正常工作,但是我在停止工作之前所拥有的代码。 如果您能告诉我我做错了什么以及如何解决,那将对您有很大帮助,我的代码在下面

import discord
import time

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

@client.event
async def on_message(message):
    if message.author == client.user:
        return

#spam command
    if message.content.startswith(prefix + 'spam'):
        if message.author == client.get_user(int(477483580918857755)):
            victim = str(message.content)
            victim = victim.replace(prefix + 'spam ', '')
            victim = victim.replace('<@!', '')
            victim = victim.replace('>','')
            user = client.get_user(int(victim))
            await message.channel.send("Now spamming " + user.name)
            await user.send('Automated spam session from ' + message.author.name)
            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)

            for i in range(5):
                await user.send("spam")
            
        else:
            await message.channel.send('You do not have permission to use that command!')
            await message.channel.send('Please contact wharncliff1 if you think this is a mistake')
            channel = client.get_channel(int(759798825161326593))
            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)
    
#help command
    elif message.content.startswith(prefix + 'help'):
        
        embed=discord.Embed(title="CliffBot Command List", description="List of commands for " + client.user.name)
        embed.add_field(name="Boop Command", value="Syntax - `" + prefix + "boop <user>`", inline=False)
        embed.add_field(name="Help Command", value="Syntax - `" + prefix + "help`", inline=False)
        embed.set_footer(text="Help command summoned by " + message.author.name)
        await message.channel.send(embed=embed)

        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)


#boop command
    elif message.content.startswith(prefix + "boop"):
        victim = str(message.content)
        print(str(victim))
        victim = victim.replace(prefix + 'boop ', '')
        victim = victim.replace('<@', '')
        victim = victim.replace('!', '')
        victim = victim.replace('>','')
        print(str(victim))
        user = client.get_user(int(victim))
        if str(message.author) == str(user):
            await message.channel.send('No! I will not allow you to boop yourself!')
            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)

        else:
            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)

#super boop command
    elif message.content.startswith(prefix + "super-boop"):
        victim = str(message.content)
        victim = victim.replace(prefix + 'super-boop ', '')
        victim = victim.replace('<@', '')
        victim = victim.replace('!', '')
        victim = victim.replace('>','')
        if str(message.author) == str(user):
            await message.channel.send('No! I will not allow you to boop yourself!')
            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)

        else:
            await message.channel.send("Super-Booped " + user.name)
            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)
            
            for i in range(10):
                await user.send('**Boop!**')
    

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

    await client.change_presence(activity=discord.Game(name='Do ' + prefix + 'help!'))

#Console Commands
#When i add all this set of commands below, all the above stops working

@client.event
async def on_ready():
    x = int(1)
    console = str(prefix + 'console')
    while x == 1:
        ConsoleInput = input('')
        if ConsoleInput == console:
            while x == 1:
                print('------')
                print('Please Select a Module')
                print('1 - Enable/Disable Commands')
                print('2 - Announce')
                print('3 - COMING SOON')
                print('99 - Exit Console')
                print('------')
                ConsoleInput = int(input(''))
                
                #Enable/Disable Commands
                if ConsoleInput == 1:
                    print('------')
                    print('Module 1 Selected - Enable/Disable Commands')
                    print('Status: IN PROGRAMMING')

                #Announce
                elif ConsoleInput == 2:
                    print('------')
                    print('Module 2 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 == 3:
                    print('------')
                    print('Module 3 Selected - COMING SOON')

                #69 cause why not
                elif ConsoleInput == 69:
                    print('------')
                    print('nice')

                #Exit Console
                elif ConsoleInput == 99:
                    print('------')
                    print('Exiting Console')
                    print('You can restart the console by typing ' + prefix + 'console in the shell')
                    print('------')
                    break
                else:
                    print('------')
                    print('That was not an option!')
            

client.run('NzUwNjg1NTY4ODk1Mjg3Mjk3.X0-Iag.HmGd4NmTS-WwYgj40cWrSQpGao0')

注意-我已经重置了令牌。

1 个答案:

答案 0 :(得分:0)

这看起来像是重复的名称问题和用户输入问题。在使用两个重复的on_ready异步函数进行测试之后,discord.py似乎中断了,或者没有任何东西可以处理重复的on_ready重复函数。

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print('USER ID: ' + str(client.user.id))
    # the rest of the code from the first on_ready function

@client.event
async def on_ready():
    x = int(1)
    console = str(prefix + 'console')
    # the rest of the code from the second on_ready function

通过结合两个on_ready函数可以轻松解决此问题。

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print('USER ID: ' + str(client.user.id))
    # the rest of the code from the first on_ready function

    c def on_ready():
    x = int(1)
    console = str(prefix + 'console')
    # the rest of the code from the sencond on_ready function

但是,从您的代码看来,您似乎有一些控制台命令所需的用户输入。在python中,无论何时您要求输入,控制台/代码都会暂停,直到用户输入了输入。您可能需要使用asyncio来解决此问题。或者最好的方法是制作2个单独的bot文件,一个用于控制台命令,另一个用于不和谐命令。