discord.py (on_reaction_add) 中的自定义表情符号无效
Custom emoji in discord.py (on_reaction_add) not working
对不起我的英语不好,我是法语:P
我正在 python 中做一个 discord 机器人(我是 python 中的初学者)并且我正在逐步进行。现在,我希望机器人在用户点击表情符号时做出反应。我使用了 3 个自定义表情符号,它不起作用:(
为了测试,我尝试使用 :slight_smile: 并且效果很好。我不知道我是不是做错了,或者如果我不能用自定义表情符号来做...
你能帮我找出我的代码出了什么问题吗? :)
@bot.command(name="mm",help="permet de créer une invit mm+ avec !mm clé niveau heure. Exemple: !mm Boralus +14 21h00")
async def mm(ctx, arg, arg2, arg3):
embed = discord.Embed(title="Inscription pour {} en {} vers {}" .format(arg, arg2, arg3), description="Composition du groupe") #,color=Hex code
embed.add_field(name="Tank", value="<:tank:761252435720667157>tank\n", inline = False)
embed.add_field(name="Heal", value="<:heal:761252937548169246>heal\n", inline = False)
embed.add_field(name="Dps1", value="<:dps:761252937066217512>dps1\n", inline = False)
embed.add_field(name="Dps2", value="<:dps:761252937066217512>dps2\n", inline = False)
embed.add_field(name="Dps3", value="<:dps:761252937066217512>dps3\n", inline = False)
# On ajoute les emojis
sent = await ctx.send(embed=embed)
emojis=['<:tank:761252435720667157>', '<:heal:761252937548169246>', '<:dps:761252937066217512>', '' ]
for emoji in emojis:
await sent.add_reaction(emoji)
# On réagit aux emoji
@bot.event
async def on_reaction_add(reaction, user):
message = reaction.message
emoji = reaction.emoji
tank = "<:tank:761252435720667157>"
heal = "<:heal:761252937548169246>"
dps = "<:dps:761252937066217512>"
test = ""
if user.bot:
return
if emoji == tank:
print ("testtank")
elif emoji == heal:
print ("testheal")
elif emoji == dps:
print ("testdps")
elif emoji == test:
print ("smiley")
else:
return
在自定义表情符号中,您应该通过在公会聊天中执行 \'emoji'
来获取他们的 ID。如我所见,您获得了 ID,但您忘记在开头和结尾添加 <>
。
而不是
tank = ":tank:761252435720667157"
heal = ":heal:761252937548169246"
dps = ":dps:761252937066217512"
你应该使用
tank = "<:tank:761252435720667157>"
heal = "<:heal:761252937548169246>"
dps = "<:dps:761252937066217512>"
也代替了
emojis=[':tank:761252435720667157', ':heal:761252937548169246', ':dps:761252937066217512', '' ]
你应该使用
emojis=['<:tank:761252435720667157>', '<:heal:761252937548169246>', '<:dps:761252937066217512>', '' ]
这将解决您的问题。
编辑
当你使用if
语句时,像我一样做if str(emoji) == tank
、if str(emoji) == heal
、emoji
returns discord.Reaction
class知道。如果你这样做,它就会起作用。
你应该让机器人得到相应的表情对象。这样你就可以比较反应表情和你的坦克、治疗、dps 等表情。这是我的解决方案,其中还包括命令内的反应检查。如果您有任何后续问题,请随时提出。我很乐意提供帮助。
@bot.command()
async def mm(ctx, arg, arg2, arg3):
embed = discord.Embed(title="Inscription pour {} en {} vers {}" .format(arg, arg2, arg3), description="Composition du groupe") #,color=Hex code
embed.add_field(name="tank", value="<:cosmic_ok:705879274464477375>\n", inline = False) #different emotes because i didnt have yours. just switch them out
embed.add_field(name="heal", value="<:smol:672722017719549983>\n", inline = False)
embed.add_field(name="dps", value="<:shumbe:671080877782204444>\n", inline = False)
sent = await ctx.send(embed=embed)
emojis=['<:cosmic_ok:705879274464477375>', '<:smol:672722017719549983>', '<:shumbe:671080877782204444>']
for emoji in emojis:
await sent.add_reaction(emoji)
tank = bot.get_emoji(705879274464477375) #returns an emoji object. the emote id is the numbers when you type \:emotehere: on discord and send the message
heal = bot.get_emoji(672722017719549983)
dps = bot.get_emoji(671080877782204444)
def check(reaction, user): #checks if the reaction was on the embed sent by the bot and if the person who reacted is the one who invoked the command. can be changed to whatever you like
return reaction.message.id == sent.id and user == ctx.author
while True:
try: # tries to execute code after this. the except keyword executes code when an error was raised during this.
reaction, _ = await bot.wait_for('reaction_add', timeout=20.0, check=check) # the bot waits for a reaction to happen. when no reaction was added or passed the check in 20 seconds, asyncio.TimeoutError is raised.
if reaction.emoji == tank: # checks if the reaction emote equals the tank emote
print('tank')
if reaction.emoji == heal:
print('heal')
if reaction.emoji == dps:
print('dps')
except asyncio.TimeoutError: # exits the loop when no reaction was added or passed the check in 20 seconds. you can also just write pass here.
break
对不起我的英语不好,我是法语:P
我正在 python 中做一个 discord 机器人(我是 python 中的初学者)并且我正在逐步进行。现在,我希望机器人在用户点击表情符号时做出反应。我使用了 3 个自定义表情符号,它不起作用:(
为了测试,我尝试使用 :slight_smile: 并且效果很好。我不知道我是不是做错了,或者如果我不能用自定义表情符号来做...
你能帮我找出我的代码出了什么问题吗? :)
@bot.command(name="mm",help="permet de créer une invit mm+ avec !mm clé niveau heure. Exemple: !mm Boralus +14 21h00")
async def mm(ctx, arg, arg2, arg3):
embed = discord.Embed(title="Inscription pour {} en {} vers {}" .format(arg, arg2, arg3), description="Composition du groupe") #,color=Hex code
embed.add_field(name="Tank", value="<:tank:761252435720667157>tank\n", inline = False)
embed.add_field(name="Heal", value="<:heal:761252937548169246>heal\n", inline = False)
embed.add_field(name="Dps1", value="<:dps:761252937066217512>dps1\n", inline = False)
embed.add_field(name="Dps2", value="<:dps:761252937066217512>dps2\n", inline = False)
embed.add_field(name="Dps3", value="<:dps:761252937066217512>dps3\n", inline = False)
# On ajoute les emojis
sent = await ctx.send(embed=embed)
emojis=['<:tank:761252435720667157>', '<:heal:761252937548169246>', '<:dps:761252937066217512>', '' ]
for emoji in emojis:
await sent.add_reaction(emoji)
# On réagit aux emoji
@bot.event
async def on_reaction_add(reaction, user):
message = reaction.message
emoji = reaction.emoji
tank = "<:tank:761252435720667157>"
heal = "<:heal:761252937548169246>"
dps = "<:dps:761252937066217512>"
test = ""
if user.bot:
return
if emoji == tank:
print ("testtank")
elif emoji == heal:
print ("testheal")
elif emoji == dps:
print ("testdps")
elif emoji == test:
print ("smiley")
else:
return
在自定义表情符号中,您应该通过在公会聊天中执行 \'emoji'
来获取他们的 ID。如我所见,您获得了 ID,但您忘记在开头和结尾添加 <>
。
而不是
tank = ":tank:761252435720667157"
heal = ":heal:761252937548169246"
dps = ":dps:761252937066217512"
你应该使用
tank = "<:tank:761252435720667157>"
heal = "<:heal:761252937548169246>"
dps = "<:dps:761252937066217512>"
也代替了
emojis=[':tank:761252435720667157', ':heal:761252937548169246', ':dps:761252937066217512', '' ]
你应该使用
emojis=['<:tank:761252435720667157>', '<:heal:761252937548169246>', '<:dps:761252937066217512>', '' ]
这将解决您的问题。 编辑
当你使用if
语句时,像我一样做if str(emoji) == tank
、if str(emoji) == heal
、emoji
returns discord.Reaction
class知道。如果你这样做,它就会起作用。
你应该让机器人得到相应的表情对象。这样你就可以比较反应表情和你的坦克、治疗、dps 等表情。这是我的解决方案,其中还包括命令内的反应检查。如果您有任何后续问题,请随时提出。我很乐意提供帮助。
@bot.command()
async def mm(ctx, arg, arg2, arg3):
embed = discord.Embed(title="Inscription pour {} en {} vers {}" .format(arg, arg2, arg3), description="Composition du groupe") #,color=Hex code
embed.add_field(name="tank", value="<:cosmic_ok:705879274464477375>\n", inline = False) #different emotes because i didnt have yours. just switch them out
embed.add_field(name="heal", value="<:smol:672722017719549983>\n", inline = False)
embed.add_field(name="dps", value="<:shumbe:671080877782204444>\n", inline = False)
sent = await ctx.send(embed=embed)
emojis=['<:cosmic_ok:705879274464477375>', '<:smol:672722017719549983>', '<:shumbe:671080877782204444>']
for emoji in emojis:
await sent.add_reaction(emoji)
tank = bot.get_emoji(705879274464477375) #returns an emoji object. the emote id is the numbers when you type \:emotehere: on discord and send the message
heal = bot.get_emoji(672722017719549983)
dps = bot.get_emoji(671080877782204444)
def check(reaction, user): #checks if the reaction was on the embed sent by the bot and if the person who reacted is the one who invoked the command. can be changed to whatever you like
return reaction.message.id == sent.id and user == ctx.author
while True:
try: # tries to execute code after this. the except keyword executes code when an error was raised during this.
reaction, _ = await bot.wait_for('reaction_add', timeout=20.0, check=check) # the bot waits for a reaction to happen. when no reaction was added or passed the check in 20 seconds, asyncio.TimeoutError is raised.
if reaction.emoji == tank: # checks if the reaction emote equals the tank emote
print('tank')
if reaction.emoji == heal:
print('heal')
if reaction.emoji == dps:
print('dps')
except asyncio.TimeoutError: # exits the loop when no reaction was added or passed the check in 20 seconds. you can also just write pass here.
break