Y
Y
Yossi_i2021-02-15 09:53:49
Python
Yossi_i, 2021-02-15 09:53:49

How to optimize discord bot in python?

Help optimize the bot. When 2 or more users click on an emoji, they are given a role, everything works fine here. But when they go through the channels with this role, the bot breaks down and removes and gives out roles with a huge delay. Help optimize.

@client.event
async def on_voice_state_update(member, before, after):
    role1 = discord.utils.get(member.guild.roles, id = 804852310966140978)
    role2 = discord.utils.get(member.guild.roles, id = 804852422082035712)
    role3 = discord.utils.get(member.guild.roles, id = 804852451991617627)
    role4 = discord.utils.get(member.guild.roles, id = 804852479543738408)
    role5 = discord.utils.get(member.guild.roles, id = 804852514456338432)
    role6 = discord.utils.get(member.guild.roles, id = 804852542260510720)
    role7 = discord.utils.get(member.guild.roles, id = 804852574150197269)
    role8 = discord.utils.get(member.guild.roles, id = 804852599911481364)
    role9 = discord.utils.get(member.guild.roles, id = 804852622959444000)
    role10 = discord.utils.get(member.guild.roles, id = 804852653367885844)

    etaj1 = discord.utils.get(member.guild.roles, id = 808149325137707028)
    etaj2 = discord.utils.get(member.guild.roles, id = 808149380082696202)
    etaj3 = discord.utils.get(member.guild.roles, id = 808149405529800735)
    etaj4 = discord.utils.get(member.guild.roles, id = 808149438823530496)
    etaj5 = discord.utils.get(member.guild.roles, id = 808149470864343041)
    etaj6 = discord.utils.get(member.guild.roles, id = 808149495073341481)
    etaj7 = discord.utils.get(member.guild.roles, id = 808149517470924830)
    etaj8 = discord.utils.get(member.guild.roles, id = 808149539310534656)
    etaj9 = discord.utils.get(member.guild.roles, id = 808149561335742484)
    etaj10 = discord.utils.get(member.guild.roles, id = 808149584479780865)

    if before.channel:
        if before.channel.id == 804901448931475506 or before.channel.id == 808359523508944896:
            await member.add_roles(etaj1)
        if before.channel.id == 804901621095858236 or before.channel.id == 804832699151810561:
            await member.add_roles(etaj2)
        if before.channel.id == 804901679718858752:
            await member.add_roles(etaj3)
        if before.channel.id == 804901679718858752:
            await member.add_roles(etaj3)
        if before.channel.id == 804901764355981322:
            await member.add_roles(etaj4)
        if before.channel.id == 804901803757404172:
            await member.add_roles(etaj5)
        if before.channel.id == 804901852712927293:
            await member.add_roles(etaj6)
        if before.channel.id == 804901904559636531:
            await member.add_roles(etaj7)
        if before.channel.id == 804901958522765322 or before.channel.id == 804735066222755871 or before.channel.id == 804736218864418816:
            await member.add_roles(etaj8)
        if before.channel.id == 804902015384682556 or before.channel.id == 804736018804244530 or before.channel.id == 804826989243924552:
            await member.add_roles(etaj9)
        if before.channel.id == 804902071421763594:
            await member.add_roles(etaj10)

    if after.channel:
        await member.remove_roles(etaj1)
        await member.remove_roles(etaj2)
        await member.remove_roles(etaj3)
        await member.remove_roles(etaj4)
        await member.remove_roles(etaj5)
        await member.remove_roles(etaj6)
        await member.remove_roles(etaj7)
        await member.remove_roles(etaj8)
        await member.remove_roles(etaj9)
        await member.remove_roles(etaj10)
    
    if after.channel:
        if after.channel.id == 804901448931475506:
            await member.remove_roles(role1)
        if after.channel.id == 804901621095858236:
            await member.remove_roles(role2)
        if after.channel.id == 804901679718858752:
            await member.remove_roles(role3)
        if after.channel.id == 804901764355981322:
            await member.remove_roles(role4)
        if after.channel.id == 804901803757404172:
            await member.remove_roles(role5)
        if after.channel.id == 804901852712927293:
            await member.remove_roles(role6)
        if after.channel.id == 804901904559636531:
            await member.remove_roles(role7)
        if after.channel.id == 804901958522765322:
            await member.remove_roles(role8)
        if after.channel.id == 804902015384682556:
            await member.remove_roles(role9)
        if after.channel.id == 804902071421763594:
            await member.remove_roles(role10)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-02-15
@Yossi_i

Delete all roles at once, why one by one

if after.channel:
        await member.remove_roles(etaj1, etaj2, etaj3, ...)

Well, I would not immediately make 20 requests to get all the roles, if the condition is met before.channel, then it does not use any of the 10 variables that took time to create.
Well, it would be nice to use elif in the if construct, so as not to check conditions a hundred times that will definitely not be fulfilled.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question