#πŸ”’ Count reactions and ban message when certain amount of a certain reaction are hit.

218 messages Β· Page 1 of 1 (latest)

toxic ravine
#
import asyncio
import discord
from discord.ext import commands
import random

TOKEN = 'youre not seeing this shit lmao'  # the account token of AVNDB

intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents, case_insensitive=True)


@bot.event
async def on_ready():
    print(f'We have logged in as {bot.user}!')  # prints a message that shows the bot is online.
#  above code logs the bot online

@bot.listen()
async def on_message(message):
    hoontee = {71372475925008384}

    username = str(message.author)  # username variable
    user_message = str(message.content)  # user message variable
    channel = str(message.channel.name)  # channel variable
    guild = str(message.guild.name)  # server variable (guild = server)
    user_ids_print = str(message.author.id)
    
    print(f'{username} ({user_ids_print}): {user_message}  (posted in "#{channel}" in "{guild}")')  # when a message is sent, it enters this in the terminal.
    #  i.e "noradrenalines: Hello. (posted in #fortnite in Fortnite Official)

    if message.author == bot.user:
        return  # ignores below code if message author is the bot
    
    if message.author.id in hoontee:
        await message.add_reaction('πŸ«ƒ')

bot.run(TOKEN)```
karmic sundialBOT
#

@toxic ravine

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

toxic ravine
#

dont worry about the reason

#

inside joke bot

#

but

#

how do i make the bot autoban the person who sent a message if a message they send get a certain amount of a reaction (for example 🀰)

visual hull
#

bot.listern is not

#

the correct function

#

bot.event

jagged bane
#

bot.listen is fine

visual hull
#

since when?

toxic ravine
#

im gonna be honest i copied some of my old bot code from

#

2021? 2022?

visual hull
#

:sob

jagged bane
#

This is fine for listening to events

toxic ravine
#

man someone told me to use bot.listen in 2022

jagged bane
#

!d discord.on_raw_reaction_add

karmic sundialBOT
#

discord.on_raw_reaction_add(payload)```
Called when a message has a reaction added. Unlike [`on_reaction_add()`](https://discordpy.readthedocs.io/en/latest/api.html#discord.on_reaction_add), this is called regardless of the state of the internal message cache.

This requires [`Intents.reactions`](https://discordpy.readthedocs.io/en/latest/api.html#discord.Intents.reactions) to be enabled.
visual hull
#

i have dpy code somewhere from like 2020

toxic ravine
#

and said bot.event is SHIT

#

what is raw reaction?

jagged bane
#

Documentation explains when it fires

visual hull
#

aaa

toxic ravine
#

"this is called regardless of the state of the internal message cache."

#

cool

#

so its always called

jagged bane
#

yup, regular on_reaction_add only fires on messages in the cache

toxic ravine
#

uhhh intents.reactions is enabled in theee

jagged bane
#

which maybe is what you want but often trips people up

toxic ravine
#

the discord developer hub

#

right

jagged bane
#

intents are declared in code

#

privileged intents are enabled on the portal, and reactions isn't a privileged one

toxic ravine
#

on_raw_reaction_add(🀰 ) should work then

#

or would it need to be a string

jagged bane
#

that's not how listening on events works

#

it always receives the same parameters. Inside of the listener you decide what you want to do and when

toxic ravine
#

ah

jagged bane
#

you receive a Reaction and a Member/User

toxic ravine
#

man these docs are a little confusing for me

#

one second

toxic ravine
#

assuming its not in default

toxic ravine
#

message.author?

#

or however it is on dpy

jagged bane
#

reaction is the Reaction object, which has a bunch of information

toxic ravine
#

oh

#

im stupid then

jagged bane
#

Oh that's on the regular event, not the raw one mb

toxic ravine
#

do you mind giving an example?

jagged bane
#

the raw one you get the full payload

toxic ravine
#

this is a little confusing

jagged bane
#

Are you relying on this message being in the cache or not?

toxic ravine
#

nope

#

it is just

#

if a guy sends a message

#

and enough people react it

#

with an emoji

#

they get banned

jagged bane
#

Right, and do you expect this to work on old messages

toxic ravine
#

mmmm

#

yeah

#

sure

#

old messages assuming like

#

just old messages

#

or is therea meaning to that

jagged bane
#

the library only caches a limited set of recent messages. on_reaction_add will only fire if people react to one of those messages. on_raw_reaction_add will fire on any message, but have less data

visual hull
#

old = like 30d+

jagged bane
#

It's based on the number of messages the bot sees, not necessarily their age

toxic ravine
#

yea]

#

sure

jagged bane
#

By default it's 1000 messages. If you want to listen on older messages, you'll want the raw event

toxic ravine
#

okay

#

thats fine

#

just keep it normal

#

if it complicates things less

jagged bane
#

Then you would implement on_reaction_add, which has parameters for Reaction and User (being the person who reacted)

#

!d discord.on_reaction_add

karmic sundialBOT
#

discord.on_reaction_add(reaction, user)```
Called when a message has a reaction added to it. Similar to [`on_message_edit()`](https://discordpy.readthedocs.io/en/latest/api.html#discord.on_message_edit), if the message is not found in the internal message cache, then this event will not be called. Consider using [`on_raw_reaction_add()`](https://discordpy.readthedocs.io/en/latest/api.html#discord.on_raw_reaction_add) instead.

Note

To get the [`Message`](https://discordpy.readthedocs.io/en/latest/api.html#discord.Message) being reacted, access it via [`Reaction.message`](https://discordpy.readthedocs.io/en/latest/api.html#discord.Reaction.message).

This requires [`Intents.reactions`](https://discordpy.readthedocs.io/en/latest/api.html#discord.Intents.reactions) to be enabled.

Note

This doesn’t require [`Intents.members`](https://discordpy.readthedocs.io/en/latest/api.html#discord.Intents.members) within a guild context, but due to Discord not providing updated user information in a direct message it’s required for direct messages to receive this event. Consider using [`on_raw_reaction_add()`](https://discordpy.readthedocs.io/en/latest/api.html#discord.on_raw_reaction_add) if you need this and do not otherwise want to enable the members intent.
jagged bane
#

So just

@bot.listen()
async def on_reaction_add(reaction: Reaction, user: Member):
  ...
toxic ravine
#

what would user be

#

message.author

#

no message not defined

#

can i add it into my
@bot.listen() async def on_message(message):

#
@bot.listen()
async def on_reaction_add(reaction: ('🀰'), user: message.author):```
#

this is NOt working...

#

idk if i formatted it wrong

jagged bane
#

that's not what I said at all

#

you can't change the parameters

toxic ravine
#

oh

#

i see

jagged bane
#

you always get those parameters in that event

toxic ravine
#

i am stupid

#

sorry

jagged bane
#

nwnw

toxic ravine
#

reaction not defined, member not defined

#

so it isnt in the common defines

#

so to line 8

#

i add

#
intents = discord.Intents.default(), discord.Intents.reactions()```
#

nope

#

hmm

#

let me try and find out

#

how to fix

toxic ravine
#

yeah

#

thought so

#

still not defined

visual hull
toxic ravine
#

i found this on SO

#
@client.event
async def on_raw_reaction_add(payload):
    if payload.channel_id == 614467771866021944:
        if payload.emoji.name == "πŸ”":
            channel = client.get_channel(payload.channel_id)
            message = await channel.fetch_message(payload.message_id)
            reaction = get(message.reactions, emoji=payload.emoji.name)
            if reaction and reaction.count > 4:
                await message.delete()```
jagged bane
#

if you want to use the raw event, sure

toxic ravine
jagged bane
#

typehinting isn't particularly needed

visual hull
#

no. user: discord.Member

jagged bane
visual hull
toxic ravine
#

hmm

#

but i dont want it to be in a specific channel

#

i want it to be in all channels

#

so i just delete line 3

visual hull
#

then remove the first if

jagged bane
#

Then don't check the channel

toxic ravine
#

yeah

#

ok

visual hull
#

im gonna go to bed. good luck with ur code

toxic ravine
#

replace client with bot

#

right

visual hull
#

yes

toxic ravine
#

does this work

#
async def on_raw_reaction_add(payload, reaction: discord.Reaction, user: discord.Member):
     if payload.emoji.name == ":pregnant_woman:":
            channel = bot.get_channel(payload.channel_id)
            message = await channel.fetch_message(payload.message_id)
            reaction = discord.get(message.reactions, emoji=payload.emoji.name)
            if reaction and reaction.count > 20:
                await message.author.ban()```
#

get didnt work on its own

jagged bane
#

you can't change the parameters.

toxic ravine
jagged bane
#

on_raw_reaction_add only gets payload

#

on_reaction_add gets reaction, user

toxic ravine
#

"get is not defined"

jagged bane
#

code?

toxic ravine
#

maybe it was changed?

jagged bane
#

discord.get was never a thing as long as I've used the library

toxic ravine
#

then what is get

#

man

jagged bane
#

you wrote it lol

toxic ravine
jagged bane
#

what are you trying to do with that line?

toxic ravine
#

wait did i not send the stackoverflow thing

#

oh

#

i forgot to import

#

wow

#

im an idito

#

ok lets see

#

should work now

jagged bane
#

You don't need to count all emojis on a message

#

you just care about the current emoji being reacted

toxic ravine
#

yes

jagged bane
#

that's on the payload

toxic ravine
#

wdym

#

so itd be

#

if reaction > 20

#

ban

#

instead of and reaction count

#

im confused

jagged bane
#

why are you back on the raw event

#

do you want it or no?

toxic ravine
#

huh

#

yeah

jagged bane
#

then the payload only gives you the current reaction happening. You won't have metadata on the message, like other reactions on it

#

you would need to fetch the message to do that

toxic ravine
#

ok so what do i do ?
sorry english isnt really my first language

#

i dont understand if i did something wrong or if youre just explaining

toxic ravine
#

or is it the other way round

#

or do i do nothing

#

idk man i misunderstand your mressage

#

sorry

jagged bane
#

So first you decide if you want to use the raw event

#

if you do, look at the payload object and see what it has

toxic ravine
#

no i know what it does

#

but i dont know how i would do it without and i feel i am very close to being done

#

i just want to finish this now and go sleep :)

jagged bane
#

So what piece of information do you want out of it?

toxic ravine
#

the amount of the 🀰 reaction

#

thats why i dont understand you

toxic ravine
#

but i do

#

no?

#

this is what is confusing me

jagged bane
#

so now what's the question? How to get the number of a certain reaction on that message?

toxic ravine
#

no

#

im asking if it works

#

like

#

if

#

will it ban someone if people react that emoji

#

on that message

#

example like this

#

20 ticks

#

and i get banned

#

that is my question

jagged bane
toxic ravine
#

no

jagged bane
#

well does it?

toxic ravine
#

i dont know man!!!!

#

thats why i came here! to look for a quicker solution than google!

#

i dont know anything, i am just using what knowledge i had a few years ago. please just jist explain a little simply on what i should do

#

no quizzes

#

sorry if that sound rude

jagged bane
#

short of writing all of the code out for you, which I'm not going to do, you should be able to debug your code and see if it's behaving like you want it to

karmic sundialBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.