多台服务器上的多条欢迎信息 discord.py

时间:2021-05-09 17:15:31

标签: discord discord.py

我是这个平台的新手,也是 Python 的新手。我想创建一个欢迎活动,向新用户问好。 (以德语显示的消息) 它工作正常,但是如果我复制事件并只放入其他 ID,它就无法正常工作。我想要多个服务器,我的机器人可以在其中向新成员问好,以及多个频道。我不知道如何让它在不止一台服务器上打招呼。我也不知道如何读取文件。没有文件的方法会很棒。但我也可以处理文件,因为如果我知道如何处理文件,我可以尝试另一个命令。这是我有 2 个服务器的代码。我想你可以看到我的尝试。但是如果我加入主服务器,它会在主服务器中发送 2 条消息,如果我加入测试服务器,它会在主服务器中发送 1 条消息。因此,它不会在测试服务器中发送任何消息。有人有想法吗?

import discord
from discord.ext import commands
import Cogs
import main
bot = commands.Bot(command_prefix=test_prefix, case insensitive=True, intents=intents

    @bot.event
    async def on_member_join(member):
        #testserver
        guild = bot.get_guild(836268774465208380)
        channel = guild.get_channel(836659340391874600)
        embed = discord.Embed(title=f'Herzlich Willkommen auf {guild.name}!', description=f'Heißen wir {member.mention} '
                                                                                          f'herzlich willkommen! Hab viel '
                                                                                          f'Spaß auf unserem Server!'
                                                                                          f' :smile:',
                              colour=discord.Colour.from_rgb(146, 4, 30))
        embed.set_footer(text=f'Bot by {botowner}', icon_url=f'{avatarowner}')
        embed.set_thumbnail(url=guild.icon_url)
        embed.set_author(name=f'{member.name}', icon_url=f'{member.avatar_url}')
        await channel.send(embed=embed)
    
    @bot.event
    async def on_member_join(member):
        #my main server
        guild = bot.get_guild(572545560758976514)
        channel = guild.get_channel(572545561371213826)
        embed2 = discord.Embed(title=f'Herzlich Willkommen auf {guild.name}!', description=f'Heißen wir {member.mention} '
                                                                                          f'herzlich willkommen! Hab viel '
                                                                                          f'Spaß auf unserem Server!'
                                                                                          f' :smile:',
                              colour=discord.Colour.from_rgb(146, 4, 30))
        embed2.set_footer(text=f'Bot by {botowner}', icon_url=f'{avatarowner}')
        embed2.set_thumbnail(url=guild.icon_url)
        embed2.set_author(name=f'{member.name}', icon_url=f'{member.avatar_url}')
        await channel.send(embed=embed2)

bot.run(test_token)

1 个答案:

答案 0 :(得分:0)

首先,您在声明 ) 的行的末尾缺少 bot,并且这两个 on_member_join 都应该是有意的,我认为这可能是复制错误。 其次,每个事件只能有一次,因此您必须将它们合并为一个。 第三,针对你的问题。如果您计划在某个时候向公众实现您的机器人,您将不得不使用一个文件,这样您就可以通过命令更改每个公会中的欢迎频道,而不必在机器人每次重新启动时都重新执行此操作。要读取 json 文件,请使用

with open("your_json_file.json", "r") as json_file:
    json_dict = json.load(json_file) #this gets the contents of the json file and loads them to a dict object.

并写入 json 文件:

with open("your_json_file.json", "w") as json_file:
    json.dump(json_dict, json_file) #here you change to contents of json_file to json_dict

如果您希望公会所有者能够更改他们的欢迎频道,则必须执行一个命令来更改它,该命令首先读取新的欢迎频道,然后将其写入 json 文件。在这段代码中,我将使用静态字典,每次机器人重新启动时都会重置它。您还必须阅读 on_member_join 事件顶部的 json 文件。 现在的代码: 您可以像这样在代码顶部声明 welcome_channels

welcome_channels = {"First Guild id here":"First guild welcome channel id here", "Second Guild id here":"Second guild welcome channel id here", etc}

那么,由于您只能使用一个 on_member_join 事件,请这样做:

@bot.event
async def on_member_join(member):
    #maybe read json file here
    if member.guild.id in list(welcome_channels.keys):
        guild = member.guild
        channel = guild.get_channel(welcome_channels[member.guild.id])
        #The rest of your code can stay the same, except remove the second on_member_join

参考文献