Как мне получить идентификатор гильдии в discord.py?
Я пытаюсь сделать некоторые серверные команды для частного бота, но я не могу понять, как привести идентификатор гильдии в состояние, которое я могу использовать для сравнения. Бот находится на нескольких частных серверах, но команды в основном являются шутками, поэтому они должны работать только на определенных серверах.
Мой текущий код таков:
Команда и ответы на данный момент являются заполнителями, просто примерами.
Код здесь всегда отвечал оператором else, даже если он находится в соответствующей гильдии. Есть идеи?
2 ответа
Вы имеете в виду сам класс, а не экземпляр. Получить ID гильдии
Вы делаете это так:
Если вы пытаетесь сделать это в событии on_message , это будет выглядеть так:
How do I access the current guild name and ID that my bot is running in?
You can just use message.guild to get the full guild object. If you want the name or id you can use this object to get them.
-
The Overflow Blog
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.5.12.43428
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Setting up the Discord Backend
The Discord backend works similar to Slack’s; messages are received from Discord via WebSocket constantly monitoring the channels that the bot has been added to. Responses are sent back to Discord via REST API call. Listing channels and users are also handled via REST.
Prerequisites
- A Discord account and your own Discord server
- If you haven’t created one yet, go to Discordapp.com and create one.
- If you do have one, log in to your account and open up the server in which you want your bot to live.
Create an Application on Discord
First, you will need to create an application on Discord. Open a text editor to copy some information down as you follow these steps:
- Open https://discordapp.com/developers/applications to view the Applications section on the Discord Developer Portal
- Log in to your account if you need to
- Click the purple New Application button on the top-right of the page to create a new application:
3. Provide a memorable name for your bot when prompted, then click the purple Create button on the bottom-right:
4. You will be placed on the General Information page for your application. Copy the Client ID listed on this page to your text editor for later 5. On the left-hand menu, click the Bot section, then click the purple Add Bot button:
6. You will be placed on the Build-A-Bot page.Authorize your Bot
With your text editor still open, follow these steps:
On the Build-A-Bot page.
- Set your Bot’s username to something other than the application name, if desired. This is the username your Discord users will see.
- Under Token , click the blue link to Click to Reveal Token . Copy the Token listed and paste it on your text editor for later
- Recommended: Under Public Bot , click the purple toggle to disable making the bot publicly available. This will ensure that only you can join the Bot to servers.
- Leave Requires OAuth2 Code Grant un-toggled.
- The bottom Bot Permissions section is where we can calculate the appropriate permissions integer to use when authorizing your Bot to your server shortly.
- If you are testing, you can select Administrator ( 8 )
- PoshBot will need at least the following permissions to run in Discord:
- View Channels
- Send Messages
- Read Message History
- Add Reactions
- Recommended additional permissions:
- Send TTS Messages
- Embed Links
- Attach Files
- Mention Everyone
- Once you have your permissions selected, copy the Permissions Integer listed and paste it on your text editor for later
- In a new browser tab, go to the below link, replacing CLIENTID with the Client ID copied from the General Information page and PERMISSIONINTEGER with the Permission Integer copied in the last step: https://discordapp.com/oauth2/authorize?&client_id=CLIENTID&scope=bot&permissions=PERMISSIONINTEGER
- Your URL should look something like this, but with your client ID and permissions integer in place of the ones added below: https://discordapp.com/oauth2/authorize?&client_id=000000000000000001&scope=bot&permissions=8
- That’ll take you to a website where you can tell Discord where to send your bot. You’ll know it worked if you open Discord in an app or in your browser and navigate to your server. The channel will say a bot has joined the room, and you’ll see it on the right side menu under the list of members.
Find your Guild ID (Server ID)
You will need the full Server ID (not the name) to continue. Follow these steps to find your Server ID:
- In Discord, open your User Settings by clicking the Settings Cog next to your user name on the bottom.
- Go to Appearance and enable Developer Mode under the Advanced section, then close User Settings.
- Open your Discord server, right-click on the server name, then select Copy ID
- Paste the Server ID on your text editor for later
- If desired, you can disable Developer Mode now.
Create PoshBot Startup Script
The following example script shows how to create a new Discord backend using the values create above.
Note that with the Discord backend, Bot Admin names should be JUST the username, i.e. superman . Do not include the @ before (e.g. @superman ) or the unique identifier (e.g. superman#1234 ), otherwise permissions will fail to resolve correctly.
Start PoshBot
Once the backend has been created, create a new instance of PoshBot and start it.
Guild id discord где найти
Reddit and its partners use cookies and similar technologies to provide you with a better experience.
By accepting all cookies, you agree to our use of cookies to deliver and maintain our services and site, improve the quality of Reddit, personalize Reddit content and advertising, and measure the effectiveness of advertising.
By rejecting non-essential cookies, Reddit may still use certain cookies to ensure the proper functionality of our platform.
For more information, please see our Cookie Notice and our Privacy Policy .