python - 如何使用 discord.PartialEmoji/discord.Emoji 对象?
问题描述
我需要一个表情符号作为反应角色的表情符号对象,但无论我做什么我都会得到错误
discord.ext.commands.errors.PartialEmojiConversionFailure:无法将“”转换为 PartialEmoji。
如果我使用 discord.PartialEmoji 或
discord.ext.commands.errors.EmojiNotFound:找不到表情符号“”。
如果我使用 discord.Eomji。
发生这些错误的示例代码
@commands.command()
async def test(self,ctx,emoji: discord.Emoji = None):
await ctx.send(emoji)
编辑:
首先应该像这样创建反应角色:
@commands.command()
async def reactionrole(self,ctx,msgid: int = None,emoji: discord.Emoji = None , role: discord.Role = None ):
if msgid is None:
await ctx.send('Use the following template to create reactionroles (ex. treactionrole <#messageid> <emoji> <@role>)')
elif emoji is None:
await ctx.send('Use the following template to create reactionroles (ex. treactionrole <#messageid> <emoji> <@role>)')
elif role is None:
await ctx.send('Use the following template to create reactionroles (ex. treactionrole <#messageid> <emoji> <@role>)')
else:
if ctx.message.author.guild_permissions.manage_roles:
db = sqlite3.connect("db.sqlite")
cursor = db.cursor()
sql = ("INSERT INTO reaction_role(message_id,emoji,role) VALUES(?,?,?)")
val = (msgid,emoji,str(role))
msg = await ctx.channel.fetch_message(int(msgid))
await msg.add_reaction(emoji)
cursor.execute(sql,val)
db.commit()
cursor.close()
db.close()
else:
await ctx.author.send('You dont have the permission to use reactionroles on this Server!')
然后,如果发生反应,一种方法应该从数据库中获取消息,其中 msgid 和表情符号与反应的相同,如下所示:
async def reaction_roles_role(msgid,emoji):
db = sqlite3.connect("db.sqlite")
cursor = db.cursor()
cursor.execute(f"SELECT role FROM reaction_role WHERE message_id = {msgid} and emoji = {emoji}")
result = cursor.fetchone()
return result
解决方案
您是为此使用默认表情符号还是来自其他服务器的表情符号?因为 discord.Emoji 仅适用于来自服务器的自定义表情符号,并且您的机器人在上面。
如果您想获得所有这些,则必须执行以下操作:
@commands.command()
async def test(self, ctx, emoji):
print(str(emoji))
对于反应角色,您可以on_raw_reaction_add
像这样比较:
if str(payload.emoji) == str(your_emoji_here):
推荐阅读
- c# - 为什么 Respawn 会擦除我的整个数据库?
- javascript - Nodejs execSync 工作但不工作,更改为系统不生效
- android - Android 依赖问题 - ApachePOI & fasterxml
- c++ - 为什么我可以使用类型别名声明 const 引用?
- vb.net - 如何删除最近添加的控件?
- c++ - 使用类模板参数推导制作静态接口
- nginx - nginx lua body_filter_by_lua_block 需要在 content_by_lua 的上下文中执行 sleep API disabled*
- guice - 如何绑定动态@Named 绑定
- abap - 形参个数大于实参个数
- javascript - 在角度项目中的数组上设置间隔