'Is it possible to link the Discord server's name with its invite?
I have a list that stores dictionaries of the names of the servers my bot is in.
The following is within a for loop, so server_name
is different for each server the bot is in:
servers = [{"Name": server_name},{"Name": server_name}]
I also have a piece of code that creates an invite for each server (which is also in a for loop) and appends the invites to a list.
When I print()
out the invites list, I get this:
[<Invite code='XXX' guild=<Guild id=XXX name='XXX' shard_id=None chunked=True member_count=X> online=X members=X>]
I wanna know if there is a way to relate the server's invite to the server's name, so I can have one dictionary with the name and another with all the servers' invites.
for guild in bot.guilds:
{"Name": name}
{"Invite": invite of that server}
I tried to go through every guild
's name
in my list with the loop above, but I have no idea how to grab the invite of that server
.
Solution 1:[1]
Using JSON, you can store the details of each server in separate dictionaries and put them all in a JSON object/string.
For example, you can put the details in a nested-dictionary (a dictionary in a dictionary) like this:
{
guild.name:
{
"name": server_name,
"invite": server_invite,
"any other details (eg: its owner)": guild.owner
}
}
And then convert it to JSON using json.dumps()
, so you can save and load it again if you want.
So, your code would be something like:
json_servers_info = "{}" #Create an empty JSON object/string. Everything will be stored in this.
for guild in bot.guilds:
if discord.Permissions.create_instant_invite in guild.me.guild_permissions:
#Check if your bot has permission to create invites on the server.
#Not sure if this is the way to check it.
server_name = guild.name #Getting the guild's name
server_invite = channel.create_invite(max_age=0, max_uses=0)
#Creating an invite to the server from the channel in which the message is sent
this_servers_info = {guild.name: {
"name": server_name,
"invite": server_invite,
"any other details (eg: its owner)": guild.owner
}}
#Creating the nested-dictionary
servers_info = json.loads(json_servers_info) #Converting the JSON into a dictionary
servers_info.update(this_servers_info) #Appending the nested-dictionary to the empty one
json_servers_info = json.dumps(servers_info) #Converting the nested-dictionary back into a JSON
You can use if guild.me.guild_permissions.create_instant_invite:
instead of the second line if it doesn't work. See How do I check if my Python Discord bot has the necessary permissions?.
The parameters max_age
and max_uses
set when it will expire and how many times the invite can be used. If both they're set to 0
, the invite will never expire and can be used infinity times.
To get the information of a particular server, you would use json.loads()
and get the details of the server by indexing the nested-dictionary:
servers_info = json.loads(json_servers_info) #Convert the JSON into a dictionary
requested_info = servers_info[str(message.guild)] #Index the dictionary to get the server's details
I've made a Discord Bot with 2 commands, one to log the servers' information and another to show the info of the server one is in:
from discord.ext import commands
import discord
import json
#Imports
intent = discord.Intents()
bot = commands.Bot(command_prefix="", description="", intents=intent)
@bot.event
async def on_ready():
print("The bot's online!")
@bot.event
async def on_message(message: discord.Message):
json_servers_info = "{}"
content = message.content #The message's content
channel = message.channel #The channel in which the message is sent
if "log server information" in content:
await channel.send("Logging...")
for guild in bot.guilds:
if discord.Permissions.create_instant_invite in guild.me.guild_permissions:
server_name = guild.name
server_invite = channel.create_invite(max_age=0, max_uses=0)
this_servers_info = {guild.name: {
"name": server_name,
"invite": server_invite,
"any other details (eg: its owner)": guild.owner
}}
servers_info = json.loads(json_servers_info)
servers_info.update(this_servers_info)
json_servers_info = json.dumps(servers_info)
await channel.send("Logged!")
elif "show this servers info" in content:
servers_info = json.loads(json_servers_info)
requested_info = servers_info[str(message.guild)]
await channel.send(requested_info)
I have not tested this. This is just an example of when and where to use the code and how. Also, note that when one uses the "log server information" command, the details of all servers the bot is in will be logged, not just of the one in which the message (command) is sent.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | The Amateur Coder |