Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
758 views
in Technique[技术] by (71.8m points)

python - Is there anything to find the guild id from a message or the channel the message was sent? Discord.py

Mine, is a funny bot made for my classmates' server. I have two or three servers where the bot is, but when i tried it not to sent messages, it wasn't able to send message neither in other server (tho this is logical)
so my idea was creating an array with ids of the guilds the bot wasnt't able to talk in, and then, send the message only if the guild id wasn't part of that array. logically, i think there is nothing wrong, i don't have to make this bot public, it's just for some friends, so i would not create multiple threads for each server.
my question is, are there any ways to get the guild id from a message sent from a user or from the channel the message was sent from? or, are there other easier ways of getting the guild's id?
let me know, thanks in advice.

question from:https://stackoverflow.com/questions/65861001/is-there-anything-to-find-the-guild-id-from-a-message-or-the-channel-the-message

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

discord.Message has a bunch of attributes, the ones you're asking for are:

So, let's say you're using an on_message event, to get the guild ID:

async def on_message(message):
    guild_id = message.guild.id

To get the channel ID:

async def on_message(message):
    channel_id = message.channel.id

If you're using commands, the Context attributes are the same

guild_id = ctx.guild.id
channel_id = ctx.channel.id

Also these are basics OOP concepts, you should already know that if you're messing with discord bots - my suggestion, learn more python.

Reference:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...