【问题标题】:If not being caught disord.py如果没有被抓到 disord.py
【发布时间】:2021-07-29 14:30:15
【问题描述】:
def check(m):
        return m.author == ctx.author and ctx.channel == m.channel

for index, i in enumerate(txtare):
    await ctx.send(f'What is the {txtare[index]}')
    try:

        variable = await client.wait_for('message',timeout=time_out, check=check)
        if variable.content == '!end':
            print(variable.content)
            return
        draw.text((x_axis[index], y_axis[index]),codecs.decode(str(variable.content), "unicode_escape"),(0,0,0),font=font2)
    except asyncio.TimeoutError:
        await ctx.send("Time out!")
        return

为什么它既不停止也不承认我写了!结束 因此,解释“变量”是不和谐用户发送给机器人的内容。然后我想检查 variable.content (消息的内容),如果它说 !end 命令应该停止

【问题讨论】:

  • 欢迎来到 SO。我不熟悉 discord.py;但variable.content 是否也包括\n
  • 你能把你的问题说清楚吗?你在做什么,你做了什么,有什么问题?
  • 所以问题是,我希望命令在我执行 !end 时停止,但不幸的是,!end 没有被捕获
  • 是的@ewong \n 很重要
  • 所以本质上,您可以将!end\n!end 进行比较,这并不相同。

标签: python if-statement discord command discord.py


【解决方案1】:

尝试做:

if variable.content.strip() == '!end':
  ##code

如果这不起作用,请尝试将您的不和谐机器人初始化为机器人而不是客户端。 你可以这样做:

from discord.ext import commands
bot = commands.Bot(command_prefix='!')
##code
bot.run(TOKEN)

机器人是客户端的子类别,并带有额外的功能。例如,为了您的使用,要检测有人发送命令,您只需要做:

@bot.command(name='thing_after_prefix_to_activate_command',help='this will explain what it does, available by doing !help')
async def name(message):
  ##code

通过使用机器人,您可以解锁客户没有的潜在用途。但是,您需要用 bot 替换代码以及任何说 client 的内容(例如:client.event --> bot.event)并删除 if variable.content = '!command': 代码。

【讨论】:

  • 您可以初始化一个变量,但是您希望 bot = commands.Bot()client = commands.Bot() 都可以在这里工作,因为 OP 正在使用 ctx,所以很明显他们正在使用 @ 987654328@,它在@client.command()
猜你喜欢
  • 2022-01-22
  • 2020-11-23
  • 2011-03-15
  • 1970-01-01
  • 2018-07-12
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多