0

Sorry to bother everyone, I know this is duplicated but I don't have the rank to comment so I have to ask again, I've seen some other posts about this and all of them say you manually get the guild id and define it in on_ready() like:

@bot.event
async def on_ready():
    guild = bot.get_guild(ID_OF_GUILD) # find ID by right clicking on server icon and choosing "copy id" at the bottom
    if guild.get_member(ID_OF_MEMBER) is not None: # find ID by right clicking on a user and choosing "copy id" at the bottom
        # the member is in the server, do something #
    else:
        # the member is not in the server, do something #

But how do I get the id of the guild automatically in on_message(), I would like my bot to run in various servers and as you can understand, that wouldn't really work.

If you need more context, its a bot where you have currencies and you can transfer them between users, I want to check if the user the message.author wants to transfer money to really exists.

Thanks in advance, Noel.

noel puig
  • 63
  • 10

1 Answers1

2

In on_message(message), you can get the guild via message.guild:

@client.event #Maybe you use client or maybe bot, then you would have to change this to whatever of them you have
async def on_message(message):
    guild = message.guild
    if guild.get_member(ID_OF_MEMBER) is not None: # find ID by right clicking on a user and choosing "copy id" at the bottom
        # the member is in the server, do something #
    else:
        # the member is not in the server, do something #

To be able to use guild.get_member, you have to enable member intents in your developer dashboard in your application under bot: Member intents and at the top of your code, where you define client, change client = ... to:

intents = discord.Intents.default()
intents.members = True
client = commands.Bot(... whatever you have here, intents=intents) #this line can be different for you!

References:

LoahL
  • 2,404
  • 1
  • 9
  • 24
  • I use @client.event, what is the difference? – noel puig May 01 '21 at 13:12
  • Nothing really, just insert `client` instead of `bot` @noelpuig – LoahL May 01 '21 at 13:16
  • It always seems to print `None` , no matter if the user is in the server or not, I've been using the id, what should I input, the id or name? – noel puig May 01 '21 at 13:21
  • You only have to input the id of the member. @noelpuig – LoahL May 01 '21 at 13:22
  • well to test it out I've been manually inputing the code `guild = message.guild`, `print(guild.get_member(546937413684166676))` that user does exist and outputs `None` – noel puig May 01 '21 at 13:27
  • You have to enable member intents for this to work, see [this](https://stackoverflow.com/a/64832812/14506951) answer for a guide to do so. @noelpuig – LoahL May 01 '21 at 13:36
  • Hey! it worked, thanks! Just one last question, instread of `client = commands.Bot(intents = intents)` I have `client = discord.Client(intents = intents)`, it works but I'd like to ask what does that do? – noel puig May 01 '21 at 16:04
  • [This answer](https://stackoverflow.com/a/51235308/14506951) explains the difference between client and bot @noelpuig – LoahL May 01 '21 at 17:16