python-3.x - 多个服务器的 Discord.py 欢迎消息
问题描述
我正在制作一个我计划在多个服务器中使用的不和谐机器人。每个服务器都有不同的欢迎频道名称等等。我发出了欢迎消息,并尝试让机器人在一个名为“欢迎”的频道中发布消息,这可以解决这个问题,但没有奏效。我考虑过创建一个数据库来保存服务器所有者在服务器名称/ID 下发送给机器人的频道 ID。机器人在触发时会将服务器 ID 与数据库中的一个匹配,然后获取链接到服务器 ID 的频道 ID。但这将是 SQL 或 PostgreSQL 中的大量编码,我必须学习如何让机器人将服务器 ID 和频道 ID 保存到数据库,如何让机器人匹配服务器 ID,然后获取频道 ID并将消息发布到服务器。没有关于 discord py 机器人和为不同服务器制作欢迎消息的文档。我想知道是否有更好的方法来做到这一点,我该怎么做?
到目前为止,我对欢迎信息的了解。
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。
解决方案
如果机器人的规模要小得多,比如只有几台服务器,那么我会说使用 json 文件来保存字典并不是一个坏主意。
您可以在服务器加入服务器时将顶部文本频道的 id 保存为默认值,并让他们通过命令更改要使用的频道,这可以通过on_guild_join
事件来完成
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 文件并添加{}
到文件中
推荐阅读
- mysql - 如何以正确的方式构建此 SQL 查询?
- mysql - 如何在 MySQL 中限制 INT?
- gradle - 在 Azure Pipelines 中复制和重命名 jar 文件
- visual-studio-code - 如何在 VSC 中指定 docker-compose 的可执行路径?
- javascript - 在 Bootstrap Vue 中动态更改选择输入
- angular - Angular Playground 设置具有封装值的样式
- java - 如何让Excel或记事本从Java中的浮动数字键盘输入数字
- java - JAVA FX - 如何从窗格中删除对象?
- java - 如何遍历地图并返回所有匹配的地图?
- java - google 在 android 开发中对 kotlin 的支持