Answer the question
In order to leave comments, you need to log in
How to determine the number of different reactions that are on the last message of the bot?
Tried to write a bot for voting and got stuck at the stage of counting votes. This is how it should have been:
But with 2 Yes and 1 No, he answers the same way. Nobody knows how to make the code work?
Here is my code:
@Bot.event
async def on_raw_reaction_add(self, payload):
channel = Bot.get_channel(payload.channel_id) # получаем объект канала
message = await channel.fetch_message(payload.message_id) # получаем объект сообщения
for emoji in message:
emoji = payload.emoji
if emoji == '✅':
global Y
Y += 1
elif emoji == '❌':
global N
N += 1
async def on_raw_reaction_remove(self, payload):
channel = Bot.get_channel(payload.channel_id) # получаем объект канала
message = await channel.fetch_message(payload.message_id) # получаем объект сообщения
for emoji in message:
emoji = payload.emoji # реакция пользователя
if emoji == '✅':
global Y
Y -= 1
elif emoji == '❌':
global N
N -= 1
if Y > N:
Result = 'Принято'
elif Y == N:
Result = 'Отказано(Да = Нет)'
else:
Result = 'Отказано'
@Bot.command(pass_context= True)
@commands.has_permissions(administrator=True)
async def endvote(ctx):
emb = discord.Embed(title=f'Окончено голосование.', description = 'Результат: ' + str(Result), colour=discord.Color.purple())
message = await ctx.send(embed=emb) # Возвращаем сообщение после отправки
Y = 0
N = 0
Answer the question
In order to leave comments, you need to log in
Everything in this code is wrong:
self
Events have an argument only in modules (cogs) . And is the module itselfif Y > N: ...
is executed only once - when the file is run. It is necessary to make it either change when updating (receiving / deleting the reaction), or immediately before the output of the resultpass_context
does not exist in the current version of discord.py . The context is always passed to the command function as the first argument (except when the command is in the above "module")voting_message = None
@Bot.command()
@commands.has_permissions(administrator=True)
async def startvote(ctx):
embed = discord.Embed(...) # embed goes here
voting_message = await ctx.send(embed=embed)
@Bot.command()
@commands.has_permissions(administrator=True)
async def endvote(ctx):
msg = await bot.get_channel(voting_message.channel.id).fetch_message(voting_message.id)
y = discord.utils.get(msg.reactions, emoji="\N{WHITE HEAVY CHECK MARK}").count
n = discord.utils.get(msg.reactions, emoji="\N{CROSS MARK}").count
if y>n:
result = "Принято"
elif y==n:
result = "Отказано (да = нет)"
else:
result = "Отказано"
emb = discord.Embed(title=f'Окончено голосование.', description = 'Результат: ' + result, colour=discord.Color.purple())
return await ctx.send(embed=emb) # **Возвращаем** сообщение после отправки.
y, n = 0, 0
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question