Reaction roles discord.py

Viewed 46

im making a reaction roles event in discord.py like this

import discord
from discord.ext import commands
import datetime
import asyncio
import random
import json

from discord.ext.commands import bot, Bot


client = discord.Client(intents=discord.Intents.all())
intents = discord.Intents.default()
intents.message_content = True
command = commands.Bot(command_prefix='.', intents=intents)


@client.event
async def on_ready():
    await client.change_presence(status=discord.Status.online, activity=discord.Game('Doing What CCXLV Says'))
    print('We have logged in as {0.user}'.format(client))


@client.event
async def on_raw_reaction_add(payload):
    guild = discord.utils.find(lambda g: g.id == payload.guild_id, bot.guilds)

    if payload.emoji.name == "" and payload.message_id == 1019155450375176242:
        role = discord.utils.get(guild.roles, name="RED")
        if role is not None:
            member = discord.utils.find(lambda m: m.id == payload.user_id, guild.members)
            if member is not None:
                await member.add_roles(role)


@client.event
async def on_raw_reaction_remove(payload):
    guild = discord.utils.find(lambda g: g.id == payload.guild_id, bot.guilds)

    if payload.emoji.name == "" and payload.message_id == 1019155450375176242:
        role = discord.utils.get(guild.roles, name="RED")
        if role is not None:
            member = discord.utils.find(lambda m: m.id == payload.user_id, guild.members)
            if member is not None:
                await member.remove_roles(role)

and this shows up idk how to fix that

guild = discord.utils.find(lambda g: g.id == payload.guild_id, bot.guilds) AttributeError: module 'discord.ext.commands.bot' has no attribute 'guilds'

1 Answers

I see a number of issues in your code, that lead to the error you get. For a start :

import discord
from discord.ext import commands
import datetime
import asyncio
import random
import json

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

This is how you wish to declare your bot. Bot is inherited from Commands, and it's the bot that needs the intents.

Since you declare a bot, you wish to use its events instead of Client's ones.

@bot.event
async def on_ready():
    await client.change_presence(status=discord.Status.online, activity=discord.Game('Doing What CCXLV Says'))
    print('We have logged in as {0.user}'.format(client))


@bot.event
async def on_raw_reaction_add(payload):
    guild = discord.utils.find(lambda g: g.id == payload.guild_id, bot.guilds)
[..]

@client.event
async def on_raw_reaction_remove(payload):
    guild = discord.utils.find(lambda g: g.id == payload.guild_id, bot.guilds)
[..]

And since bot has been declared at the beginning, the "bot.guilds" here will work. In your code, since you declared "command" as the bot, it would have been "command.guilds" instead. Anyway, better to write it how I corrected it, so if you look for further help, most discord bots are declared that way.

Related