1
votes

I am coding a bot to find users with the 'Admin' role on my discord server and do something with them. So far I have written this:

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="$")
role_name = "Admin"
peopleWithRole = []

@bot.event
async def on_ready():
    print("Logged in as")
    print(bot.user.name)
    print("------")
    role = discord.utils.find(
        lambda r: r.name == role_name, guild.roles)
        
    for user in guild.members:
        if role in user.roles:
            peopleWithRole.append(user)

bot.run("My token")

However, when I run this, I get an error saying that name 'guild' is not defined. I am just starting out with discord.py and I was also wondering whether I should be using client or bot in this situation.

1

1 Answers

1
votes
import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="$")
role_name = "Admin"
peopleWithRole = []

@bot.event
async def on_ready():
    print("Logged in as")
    print(bot.user.name)
    print("------")
    guild = bot.guilds[0]

    role = discord.utils.find(
        lambda r: r.name == role_name, guild.roles)
        
    for user in guild.members:
        if role in user.roles:
            peopleWithRole.append(user)

bot.run("My token")

In your find call you reference guild.roles but have never defined guild. You need to select the guild (I believe guilds is a member of bot)

This is a fairly basic python error to debug, and any IDE would identify what is wrong. I suggest you look up some tutorials on how to debug.