首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使消息内容成为变量discord.py

使消息内容成为变量discord.py
EN

Stack Overflow用户
提问于 2021-07-28 05:39:16
回答 2查看 728关注 0票数 1
代码语言:javascript
复制
@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坏请求(错误代码: 50006):无法发送空消息

试图做出这样的回答:“你想要这个头衔是什么?”一种将作为测试返回的变量。请帮帮忙。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-07-28 06:46:25

这里是一个例子,您需要使用wait_for

代码语言:javascript
复制
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

票数 2
EN

Stack Overflow用户

发布于 2021-07-28 06:49:17

在共享代码的最后一行

代码语言:javascript
复制
await ctx.send(title)

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

代码语言:javascript
复制
title = ''

这就是问题出现的原因。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68554940

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档