【问题标题】:When I add a bit of code, the rest stops working当我添加一些代码时,其余部分停止工作
【发布时间】:2020-10-05 02:52:18
【问题描述】:

详情: 我正在为我的服务器编写一个 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')

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

【问题讨论】:

  • 请提供预期的MRE。显示中间结果与预期结果的偏差。我们应该能够将您的代码块粘贴到文件中,运行它并重现您的问题。 “停止工作”不是问题描述。执行从何而来?它停在哪里?哪些中间值让您感到惊讶?与工作版本有什么区别?您的代码依赖项有什么复杂之处,以至于 需要 200 行代码来演示?
  • @Prune 对不起,正如我所说我是新手,我会用正确的 MRE 重新发布更多细节
  • 请从intro tour 重复on topichow to ask。这就是我们进行介绍之旅的原因。
  • @Prune 这是我的更新帖子stackoverflow.com/questions/64188405/…

标签: python discord discord.py


【解决方案1】:

这看起来像是重复名称问题和用户输入问题。在使用两个重复的 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 文件,一个用于控制台命令,另一个用于不和谐命令。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    • 2012-12-09
    • 2021-07-08
    • 2017-05-16
    • 2013-07-05
    • 1970-01-01
    相关资源
    最近更新 更多