我是开发不和谐机器人的新手,遇到了一个问题。 “ 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?
答案 0 :(得分:1)
您以错误的方式使用try-except
,请看一下我在下面的代码中留下的注释。您可以here对其进行了解。
@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")