'I wanted to create a command that allows you to bind your channel for messages

It's all my code for now

My knowledge in python is not that great, but I tried to make at least something. If you have any questions I will listen to you


Which libraries i use

  • import discord

  • import json

  • from discord.ext import commands


I've been trying to figure out how to do it right for a long time. But I can't get it. Here is the code:

@bot.event
async def on_member_join(member):
    if member.guild.name == 'Polinochka':
        member_role = get(member.guild.roles, id=968393860751454209)
        channelsadg = bot.get_channel(968393861359632395)  # Where ID is your welcome channel's ID
        emb = discord.Embed(title=f'Привет {member}! Ты попал на сервер {member.guild.name}.',
                            description='Что-бы узнать мои команды пропиши **P.help** <:mmmm_yummy:953568889860685856>',
                            color=0x5865F2)
        emb.set_thumbnail(url='https://media.discordapp.net/attachments/937807107510657066/961730323933196329/1.png')
        emb.set_footer(text='Created by DanicBro12#6765')
        if member_role:
            await member.add_roles(member_role)
            await channelsadg.send(embed=emb)
        else:
            print("role could not be found")
    else:
        if member.guild.id != 'guilds.json':
            with open('guilds.json', 'r', encoding='utf-8') as f:
                guilds_dict = json.load(f)

            emb = discord.Embed(title=f'Привет {member}! Ты попал на сервер {member.guild.name}.',
                                description='Что-бы узнать мои команды пропиши **P.help** <:mmmm_yummy:953568889860685856>',
                                color=0x5865F2)
            emb.set_thumbnail(url=member.guild.icon_url)
            emb.set_footer(text='Created by DanicBro12#6765')

            channel_id = guilds_dict[str(member.guild.id)]
            await bot.get_channel(int(channel_id)).send(embed=emb)
            if 'messages.json' != 'guilds.json':
                with open('messages.json', 'r', encoding='utf-8') as f:
                    messages_dict = json.load(f)

                if channel_id in messages_dict:
                    channel_id = guilds_dict[str(member.guild.id)]
                    castom_message = messages_dict[str()]
                    await bot.get_channel(int(channel_id)).send(embed=emb)

                channel_id = messages_dict[str(member.guild.id)]
            else:
                with open('guilds.json', 'r', encoding='utf-8') as f:
                    guilds_dict = json.load(f)

                emb = discord.Embed(title=f'Привет {member}! Ты попал на сервер {member.guild.name}.',
                                    description='Что-бы узнать мои команды пропиши **P.help** <:mmmm_yummy:953568889860685856>',
                                    color=0x5865F2)
                emb.set_thumbnail(url=member.guild.icon_url)
                emb.set_footer(text='Created by DanicBro12#6765')

                channel_id = guilds_dict[str(member.guild.id)]
                await bot.get_channel(int(channel_id)).send(embed=emb)
        else:
            channel = member.guild.text_channels[0]
            emb = discord.Embed(title=f'Привет {member}! Ты попал на сервер {member.guild.name}.',
                                description='Что-бы узнать мои команды пропиши **P.help** <:mmmm_yummy:953568889860685856>',
                                color=0x5865F2)
            emb.set_thumbnail(url=member.guild.icon_url)
            emb.set_footer(text='Created by DanicBro12#6765')
            await bot.get_channel(channel).send(embed=emb)

@bot.command(name='welcome')
async def set_welcome_channel(ctx, channel: discord.TextChannel = None, role: discord.Role = None,content: discord.Message = None):
    if channel is None:
        await ctx.send('Укажите канал через #')
    else:
        if content is None:
            with open('guilds.json', 'r', encoding='utf-8') as f:
                guilds_dict = json.load(f)

            guilds_dict[str(ctx.guild.id)] = str(channel.id)
            with open('guilds.json', 'w', encoding='utf-8') as f:
                json.dump(guilds_dict, f, indent=4, ensure_ascii=False)

            await ctx.send(f'Sent welcome channel for {ctx.message.guild.name} to {channel.name}')
        else:
            if role is None:
                with open('messages.json', 'r', encoding='utf-8') as f:
                    messages_dict = json.load(f)

                messages_dict[str(ctx.guild.id)] = str(content.content)
                with open('messages.json', 'w', encoding='utf-8') as f:
                    json.dump(messages_dict, f, indent=4, ensure_ascii=False)

                await ctx.send(f'Sent welcome channel for {ctx.message.guild.name} to {channel.name}, welcome message {content.content}')
            else:
                with open('roles.json', 'r', encoding='utf-8') as f:
                    messages_dict = json.load(f)

                messages_dict[str(ctx.guild.id)] = str(content.content)
                with open('roles.json', 'w', encoding='utf-8') as f:
                    json.dump(messages_dict, f, indent=4, ensure_ascii=False)

                await ctx.send(f'Sent welcome channel for {ctx.message.guild.name} to {channel.name}, role {role.name}, welcome message {content.content}')


# Optional:
# So if your bot leaves a guild, the guild is removed from the dict
@bot.event
async def on_guild_remove(guild):
    with open('guilds.json', 'r', encoding='utf-8') as f:
        guilds_dict = json.load(f)

    guilds_dict.pop(guild.id)
    with open('guilds.json', 'w', encoding='utf-8') as f:
        json.dump(guilds_dict, f, indent=4, ensure_ascii=False)

    with open('messages.json', 'r', encoding='utf-8') as f:
        messages_dict = json.load(f)

    messages_dict.pop(guild.id)
    with open('messages.json', 'w', encoding='utf-8') as f:
        json.dump(messages_dict, f, indent=4, ensure_ascii=False)

    with open('roles.json', 'r', encoding='utf-8') as f:
        roles_dict = json.load(f)

    roles_dict.pop(guild.id)
    with open('rolees.json', 'w', encoding='utf-8') as f:
        json.dump(roles_dict, f, indent=4, ensure_ascii=False)

If there are any shortcomings, I will understand everything, I ask you to help again



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source