discord.py - “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?
解决方案
你用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")
推荐阅读
- subscribe - 提供的网络推送 applicationServerKey 无效
- javascript - 如何在所有点击完成后调用一个函数,无论是单击还是快速分离中的 10 次点击后?
- r - 在 R 中创建多边形映射
- python - 写一个递归函数,len
- symfony - Symfony API 平台过滤器
- c# - 将字节数组与 EF Core InMemoryDatabase 进行比较以进行单元测试
- templates - Xamarin MVVM 显示来自另一个模型/表/对象的数据
- android - 找不到小米手机 id 列中的 PICK_CONTACT
- python - 为什么我在尝试添加 opencv 模块时在 PyCharm 中出现错误,尽管已经安装
- apache-spark - presto with hudi - 从表中选择 *