英文:
Discord.py wait_for() not working when using Cogs-- API docs useless
问题
我试图创建一个基本命令,当用户键入/ 'command_name'后,机器人等待用户发送另一条消息(如果有一个小文本框选项就好了),然后对他们发送的消息做些什么。
class Test(commands.Cog, name="test"):
def __init__(self,bot):
self.bot = bot
@commands.hybrid_command(name='create_response', with_app_command=True)
async def create_response(self, ctx):
def check(msg):
return msg.author == ctx.author
msg = await self.bot.wait_for('message', check=check)
await ctx.send(msg)
这段代码什么也不做,有时会出错。
预期输出应该是:
用户:/create_response
机器人回复:[______](这将是他们输入/create_response命令后的文本框)
用户:“你好”
机器人回复:“你好”
英文:
I'm trying to create a basic command that, after a user types /'command_name', the bot waits for the user to send another message (it would be nice if there was an option for a little text field) and then does something with the message they send.
class Test(commands.Cog, name="test"):
def __init__(self,bot):
self.bot = bot
@commands.hybrid_command(name='create_response', with_app_command=True)
async def create_response(self, ctx):
def check(msg):
return msg.author == ctx.author
msg = await self.bot.wait_for('message', check=check)
await ctx.send(msg)
This code does nothing and sometimes(?) errors out.
The expected output would be something like:
User: /create_response
Bot reply: [______] (this would be a text box after they type the /create_response command
User: "Hello"
Bot reply: "Hello"
</details>
# 答案1
**得分**: 2
获取消息内容并使用`on_message`事件(这是您的机器人正在等待的内容),您需要启用[Intents.message_content](https://discordpy.readthedocs.io/en/stable/api.html#discord.Intents.message_content):
```python
from discord import Intents
from discord.ext import commands
# 创建具有所有功能的Intents的工厂方法
# 除了特权网关意图(出席、成员和消息内容);
# 您还需要在开发人员门户中授权特权意图;
# 为了更好地控制机器人的缓存
# 我建议使用Intents(<option>=True)来仅选择您需要的内容
intents = Intents.default()
# 启用消息内容意图
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)
您还可以将文本作为命令参数接收:
@commands.hybrid_command(name='create_response')
async def create_response(self, ctx: commands.Context, *, text: str):
"""This command creates a response
Args:
text: 输入您要发送的文本
"""
await ctx.reply(text)
- 我使用docstrings来描述命令和参数的功能。这些描述将自动被应用程序命令使用;
- 我建议阅读这篇文章,了解如何使用
discord.ext.commands
框架开发斜杠命令。
英文:
To get the contents of messages and use the on_message
event (this is what your bot is waiting for) you need to enable Intents.message_content:
from discord import Intents
from discord.ext import commands
# This factory method that creates a Intents with everything enabled
# Except the privileged gateway intents (presences, members, and message_content);
# You also need to authorize privileged intents in the developer portal;
# For greater control of the bot's cache
# I recommend using Intents(<option>=True) to select only what you need
intents = Intents.default()
# Enabling the message content intent
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)
You can also receive the text as a command parameter:
@commands.hybrid_command(name='create_response')
async def create_response(self, ctx: commands.Context, *, text: str):
"""This command creates a response
Args:
text: type the text you want to send
"""
await ctx.reply(text)
> - I'm using docstrings to describe the function of the command and arguments. These descriptions will automatically be used by the application commands;
> - I recommend reading this article on how to develop slash commands using the discord.ext.commands
framework.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论