A
A
Artyom Valitov2020-06-22 11:46:34
Python
Artyom Valitov, 2020-06-22 11:46:34

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:

5ef06f7f7d552137784749.png

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

1 answer(s)
M
Maxim Nevzorov, 2020-06-24
@artv15

Everything in this code is wrong:

  1. @Bot.event is only for add reaction event. All events must have an event decorator.
  2. selfEvents have an argument only in modules (cogs) . And is the module itself
  3. if 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 result
  4. The key (kwarg) argument pass_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")
  5. 'message' object is not iterable
  6. PEP8 Naming Conventions : "CamelCase" variable names denote classes.

One of the solutions:
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 question

Ask a Question

731 491 924 answers to any question