Discord.py welcome message for multiple servers

冷暖自知 提交于 2021-02-08 06:45:36

问题


I am making a discord bot that I plan on being in multiple servers. Each server will have a different welcome channel name and all that. I made the welcome message and I tried making the bot post the message in a channel called "welcome" which would solve this problem but didn't work. I thought about making a database that saves the channel id that the server owner sends to the bot under the server name/ID. The bot when triggered would match the server ID to one in the database then grab the channel id linked to the server id. But that would be a lot of coding in SQL or PostgreSQL which I would have to learn how to get the bot to save the sever id and channel id to the database, How to get the bot to match the server id's then grab the channel id and posting it the message to the server. There is no documentation on discord py bots and making welcome messages for different servers. I was wondering if there is a better way to do it and how would I do it?

What I have so far in relation to the welcome message.


import discord
import logging
import asyncio
import random
import time
import tweepy, discord

from discord.ext import commands
from discord.ext.commands import bot

#File Imports
from config import *


client = commands.Bot(command_prefix='sec.')

# logger = logging.getLogger('discord')
# logger.setLevel(logging.DEBUG)
# handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
# handler.setFormatter(logging.Formatter('%(name)s: %(message)s'))
# logger.addHandler(handler)

@client.event
async def on_ready():
    print('Logged in as %s' % client.user.name)
    while True:
        presence = random.choice(['sec.help', 'Defending Servers'])
        activity = discord.Game(name=(presence))
        await client.change_presence(status=discord.Status.online, activity=activity)
        await asyncio.sleep(7)

client.remove_command('help')

@client.event
async def on_member_join(member):
    # Adds role to user
    # role = discord.utils.get(member.server.roles, name='Member')
    # await client.add_roles(member, role)

    # Random embed color
    range = [255,0,0]
    rand = random.shuffle(range)

    # Welcomes User
    embed = discord.Embed(title="{}'s info".format(member.name), description="Welcome too {}".format(member.guild.name))
    embed.add_field(name="Name", value=member.name, inline=True)
    embed.add_field(name="ID", value=member.id, inline=True)
    embed.add_field(name="Status", value=member.status, inline=True)
    embed.add_field(name="Roles", value=member.top_role)
    embed.add_field(name="Joined", value=member.joined_at)
    embed.add_field(name="Created", value=member.created_at)
    embed.set_thumbnail(url=member.avatar_url)
    inlul = client.get_channel(CHANNEL_ID)

    await inlul.send(inlul, embed=embed)

If you find any documentation on this I would love to read it. All I could find are for bots that are basic and has you enter a channel id.


回答1:


If the bot is on a much smaller scale, say just a few servers, then I'd say using json file to save a dictionary wouldn't be a bad idea.

You can save the id of the top text channel as a default when the server joins the server and let them change what channel to use with commands, this can be done with the on_guild_join event

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)

then just use

with open("filename.json", "r"):
    guildInfo = json.load(f)

channnel = guildInfo[ctx.message.guild.id]

to get the channel to send the message to and

channel.send(embed=embed)

to send the message

before running it ensure to have an empty json file in the same directory and add {} to the file



来源:https://stackoverflow.com/questions/62667213/discord-py-welcome-message-for-multiple-servers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!