Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

python - how to make discord bot adapt to other server?

for my discord bot I have hard coded the channel id for the member_leave and member_join. If i wanted to make a command to make it adapt to other server such as when they do -create_join and it will create a member_join in that channel where can i get more info about this ?

I know i am asking too much but please provide me an example of how i can do this ? Many people have recommend me to use postgresql. code :

  WELCOME_ID = 788460126368825355
  LEAVE_ID = 788460140986761227
@client.event
async def on_member_join(member):

    channel = client.get_channel(WELCOME_ID)

    embed = discord.Embed(
        colour = 0x99e68b,
        title ='??????????????????? smilewin !',
        description = '????????????????????????????????????????'
        )

    embed.set_thumbnail(url=f"{member.avatar_url}")
    embed.set_author(name=f"{member.name}", icon_url=f"{member.avatar_url}") 
    embed.set_footer(text=f"{member.guild}", icon_url=f"{member.guild.icon_url}")
    embed.timestamp = datetime.datetime.utcnow()

    embed.set_footer(text='┗Powered by REACT')

    print(f"{member.name} have joined the server {member.guild.name}")
    if member.guild.id == PERSONAL_GUILD_ID:
        await channel.send(embed=embed)
    
@client.event
async def on_member_remove(member):

    channel = client.get_channel(LEAVE_ID)

    embed = discord.Embed(
        colour=0x983925, 
        title = "Member leave",
        description= f"{member.name}??????????????????"
        )

    embed.set_thumbnail(url=f"{member.avatar_url}")
    embed.set_author(name=f"{member.name}", icon_url=f"{member.avatar_url}") 
    embed.set_footer(text=f"{member.guild}", icon_url=f"{member.guild.icon_url}")
    embed.timestamp = datetime.datetime.utcnow()

    embed.set_footer(text='┗Powered by REACT')

    print(f"{member.name} have left the server {member.guild.name}")
    if member.guild.id == PERSONAL_GUILD_ID:
        await channel.send(embed=embed)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In the end what you want is a database to store those channel_id values. One way of doing so would be a JSON as another answer suggested, but if you want to add your bot and use this functionality in many servers, the best would be to use a real database like SQL.

Taking into account that discord.py uses async interface, the best library for starting would be aiosqlite (Visit https://github.com/omnilib/aiosqlite).

A simple Python code to store those channel_id in your database would be something like:

async def set_channel(self, ctx, channel:discord.TextChannel):
    database = await aiosqlite.connect(database_path)
    cursor = await database.cursor()
    sql = ("UPDATE welcome SET welcome_channel_id = ? WHERE guild_id = ?")
    val = (channel.id, ctx.guild.id)
    await cursor.execute(sql, val)
    await database.commit()
    await cursor.close()
    await database.close()

This sample of code already asumes you have your database set up with the tables created, but with a tiny bit of knowledge you should be able to modify this fine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...