【问题标题】:Making message content a variable discord.py使消息内容成为变量 discord.py
【发布时间】:2021-07-28 15:21:26
【问题描述】:
@client.command()
async def makereactionrole(ctx):
    await ctx.send('What would you like the title to be?')
    title = ''
    def check(msg):
        return msg.author == ctx.author and mesg.channel == ctx.channel
        title = msg.content
    await ctx.send(title)

错误:discord.ext.commands.errors.CommandInvokeError:命令引发异常:HTTPException:400 Bad Request(错误代码:50006):无法发送空消息

试图做出“你希望标题是什么?”的回应一个将作为测试发回的变量。请帮忙。

【问题讨论】:

  • 你能用右缩进格式化代码吗?
  • 是的,你的缩进不正确
  • @mundanehassan 应该改变什么?我的缩进从来没有遇到过错误。

标签: python discord discord.py


【解决方案1】:

你需要使用wait_for这里是一个例子。

import asyncio

@bot.command()
async def makereactionrole(ctx):
    def author_and_channel(msg):
        return msg.author == ctx.author and msg.channel == ctx.channel
    
    await ctx.send("Please enter a title")

    #timeout is in sec
    try:
        title_msg = await bot.wait_for('message',
                                       check=author_and_channel,
                                       timeout=5.0)
    except asyncio.TimeoutError:
        return await ctx.channel.send(f'Sorry, you took too long to respond')

    title = title_msg.content
    print(title)

文档: Bot.wait_for

【讨论】:

  • 该功能的目标是让它提出所有问题并获得个人答复,然后将所有问题集中在一起形成反应角色。在我添加更多问题之前,我需要做的就是弄清楚如何让第一个问题发挥作用。
  • 你找wait_for我加一下
  • 你添加到任何地方了吗?
  • 现在你可以在上面看到了:)
【解决方案2】:

在您共享代码的最后一行

await ctx.send(title)

它将title 变量作为消息发送,但由于共享代码第三行上的title 变量为空。

title = ''

这就是问题发生的原因

【讨论】:

    猜你喜欢
    • 2021-06-17
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 2021-06-27
    • 2022-12-15
    • 2020-11-24
    • 2022-08-15
    • 1970-01-01
    相关资源
    最近更新 更多