多个服务器的 Discord.py 欢迎消息 [英] Discord.py welcome message for multiple servers

查看:13
本文介绍了多个服务器的 Discord.py 欢迎消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个我计划在多个服务器中使用的不和谐机器人.每个服务器都有不同的欢迎频道名称等等.我发出了欢迎消息,并尝试让机器人在名为欢迎"的频道中发布消息.这将解决这个问题,但没有奏效.我考虑过创建一个数据库来保存服务器所有者在服务器名称/ID 下发送给机器人的频道 ID.机器人在触发时会将服务器 ID 与数据库中的一个匹配,然后获取链接到服务器 ID 的频道 ID.但这将是 SQL 或 PostgreSQL 中的大量编码,我必须学习如何让机器人将服务器 ID 和频道 ID 保存到数据库,如何让机器人匹配服务器 ID,然后获取频道 ID并将消息发布到服务器.没有关于 discord py 机器人和为不同服务器制作欢迎消息的文档.我想知道是否有更好的方法可以做到这一点,我该怎么做?

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)

如果您找到任何关于此的文档,我很乐意阅读.我能找到的只是基本的机器人,并且你输入了频道 ID.

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.

推荐答案

如果 bot 规模小得多,比如说只有几台服务器,那么我会说使用 json 文件保存字典不会是一个坏主意.

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.

您可以在服务器加入服务器时将顶部文本频道的 id 保存为默认值,并让他们更改使用命令的频道,这可以通过 on_guild_join 事件来完成

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)

然后就用

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

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

获取发送消息的频道和

channel.send(embed=embed)

发送消息

在运行之前确保在同一目录中有一个空的json文件并将{}添加到文件中

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

这篇关于多个服务器的 Discord.py 欢迎消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆