welcome system 1

welcome system

import json

#sets value in json to guild id upon the bot joining the guild
@client.event
async def on_guild_join(guild):
    #loads json file to dictionary
    with open("filename.json", "r") as f:
        guildInfo = json.load(f)

    guildInfo[guild.id] = guild.text_channels[0] #sets key to guilds id and value to top textchannel
    
    #writes dictionary to json file
    with open("filename.json", "w" as f:
        json.dump(guildInfo, f)

#allows server members to set channel for welcome messages to send to    
@client.command()
async def welcomeMessage(ctx):
    with open("filename.json", "r") as f:
        guildInfo = json.load(f)

    guildInfo[ctx.message.guild.id] = ctx.message.channel.id #sets channel to send message to as the channel the command was sent to

    with open("filename.json", "w") as f:
        json.dump(guildInfo, f)

Here is what the above code is Doing:
1. When the bot joins a guild, it will add the guild id to a json file with the value of the top text channel in the guild
2. When a user sends the command “welcomeMessage” in a text channel, the bot will set the value of the guild id in the json file to the channel the command was sent in
3. When a user joins the guild, the bot will send a message to the channel that was set in the json file

Similar Posts