首页 > 解决方案 > “except”函数不适用于 discord.py

问题描述

我是开发不和谐机器人的新手,遇到了一个问题。"except" 函数不工作并出现语法错误。

它绘制的错误在这里:

文件“bot.py”,第 36 行,除了 MissingRequiredArgument:^ SyntaxError:无效语法

代码在这里:

import random
from discord.ext import commands

client = commands.Bot(command_prefix = '.')
@client.event
async def on_ready():
    print("Bot is ready!")
    await client.change_presence(activity = discord.Game("with fire"))

@client.command()
async def eightball(ctx, *, question):
    repsonses = [ 'It is certain.',
    'It is decidedly so.',
    'Without a doubt.',
    'Yes – definitely.',
    'You may rely on it.',
    'As I see it, yes.',
    'Most likely.',
    'Outlook good.',
    'Yes.',
    'Signs point to yes.',
    'Reply hazy, try again.',
    'Ask again later.',
    'Better not tell you now.',
    'Cannot predict now.',
    'Concentrate and ask again.',
    "Don't count on it.",
    'My reply is no.',
    'My sources say no.',
    'Outlook not so good.',
    'Very doubtful.']
    embed = discord.Embed(title="Fireplace", description=(f'{repsonses[random.randint(0, 20)]}'), colour = discord.Colour.orange())
    embed.set_footer(text = " By logiccc", icon_url="https://pbs.twimg.com/media/C6F-YfVUwAE91mv.jpg")
    await ctx.send(embed=embed)
except:
    embed = discord.Embed(title="Fireplace", description=("Sorry! Something went wrong... The command syntax is .eightball <question>"), colour = discord.Colour.orange())
    embed.set_footer(text = " By logiccc", icon_url="https://pbs.twimg.com/media/C6F-YfVUwAE91mv.jpg")
@client.command()
async def ping(ctx):
    embed = discord.Embed(title="Fireplace", description="Pong!", colour = discord.Colour.orange())
    embed.set_footer(text = " By logiccc", icon_url="https://pbs.twimg.com/media/C6F-YfVUwAE91mv.jpg")
    await ctx.send(embed=embed)```
I have no idea why this happens. Anyone else know?
Edit: I have tried the code in the comment but it doesn't work. I just keep getting the error message in the console and no reply from the bot. Any tips?

标签: discord.py

解决方案


你用try-except错了方法,看看我在下面代码中留下的评论。你可以在这里了解它。

@client.command()
async def eightball(ctx, *, question):
    repsonses = ['It is certain.',
                 'It is decidedly so.',
                 'Without a doubt.',
                 'Yes – definitely.',
                 'You may rely on it.',
                 'As I see it, yes.',
                 'Most likely.',
                 'Outlook good.',
                 'Yes.',
                 'Signs point to yes.',
                 'Reply hazy, try again.',
                 'Ask again later.',
                 'Better not tell you now.',
                 'Cannot predict now.',
                 'Concentrate and ask again.',
                 "Don't count on it.",
                 'My reply is no.',
                 'My sources say no.',
                 'Outlook not so good.',
                 'Very doubtful.']
    try:  # Try to do this 
        embed = discord.Embed(title="Fireplace", description=(f'{repsonses[random.randint(0, 20)]}'),
                              colour=discord.Colour.orange())
        embed.set_footer(text=" By logiccc", icon_url="https://pbs.twimg.com/media/C6F-YfVUwAE91mv.jpg")
        await ctx.send(embed=embed)
    except:  # If can't then do this
        embed = discord.Embed(title="Fireplace", description=(
            "Sorry! Something went wrong... The command syntax is .eightball <question>"),
                              colour=discord.Colour.orange())
        embed.set_footer(text=" By logiccc", icon_url="https://pbs.twimg.com/media/C6F-YfVUwAE91mv.jpg")

推荐阅读