我有一个问题。我如何检查用户使用了哪个表情符号?如何检查特定用户是否对特定消息有反应[discord.py]
我想检查反应是✅
还是❌
文件夹结构
├── main.py
├── cogs
│ ├── member.py
问题是我没有得到错误消息。什么也不会发生。当我用表情符号回应时,什么也没发生。
member.py
import discord
from discord.ext import commands
from datetime import datetime
class command(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_message(self, message):
...
...
for emoji in emojis:
await msg.add_reaction(emoji)
await message.author.send('Send me that ✅ reaction, mate')
def check(reaction, user):
return user == message.author and str(reaction.emoji) == '✅'
res = await self.bot.wait_for(
"reaction_add",
check=check, timeout=None
)
print(res.emoji)
if res.content.lower() == "✅":
await message.author.send("Got it")
else:
await message.author.send("Thanks")
confirmation = await self.bot.wait_for("reaction_add", check=check)
await message.author.send("You responded with {}".format(reaction.emoji))
async def setup(bot):
await bot.add_cog(command(bot))
import asyncio
import os
from dotenv import load_dotenv
import discord
from discord.ext import commands
from discord.ext.commands import has_permissions
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
import discord
from discord.utils import get
class MyBot(commands.Bot):
def __init__(self):
intents = discord.Intents.default()
intents.message_content = True
super().__init__(command_prefix=commands.when_mentioned_or('-'), intents=intents, max_messages=1000)
async def on_ready(self):
print(f'Logged in as {self.user} (ID: {self.user.id})')
async def setup_hook(self):
for file in os.listdir("./cogs"):
if file.endswith(".py"):
extension = file[:-3]
try:
await self.load_extension(f"cogs.{extension}")
print(f"Loaded extension '{extension}'")
except Exception as e:
exception = f"{type(e).__name__}: {e}"
print(f"Failed to load extension {extension}n{exception}")
bot = MyBot()
bot.run(TOKEN)
您正在使用一个名为check
的函数,它不存在-就像您的错误告诉您一样。在你的类中有一个,但它不在同一个作用域中,所以你不能用它的名字来调用它。
访问类中的方法,使用self.<name>
。
同样,您应该只传递检查函数,而不是调用。
(..., check=self.check
编辑原问题中的代码经过编辑。您没有异步加载齿轮。扩展,在2.0中,齿轮是异步的。文档:https://discordpy.readthedocs.io/en/stable/migrating.html extension-and-cog-loading-unloading-is-now-asynchronous
将此代码放入@commands.Cog.listener()
装饰器中,如果cogs加载器正常工作,该代码将正常工作。如果你想让我给你看看我的装载机,我可以。
accept = '✅'
decline = '🚫'
def check(reaction, user):
return user == author
messsage = await ctx.send("test")
await message.add_reaction(accept)
await message.add_reaction(decline)
reaction = await self.bot.wait_for("reaction_add", check=check)
if str(reaction[0]) == accept:
await ctx.send("Accepted")
elif str(reaction[0]) == decline:
await ctx.send("Denied")