慢的Python discord.py脚本

huangapple go评论69阅读模式
英文:

Slow Python discord.py script

问题

有人知道为什么这个Python脚本运行得这么慢吗

当我开始调用await member.remove_roles()和await member.add_roles()时它开始变慢当我注释掉这两行时性能看起来还可以

一些背景信息我想要实现的目标是
用户通过网站订阅这些数据存储在MySQL数据库中根据订阅他们会被分配一个用户角色
根据用户角色我想在Discord服务器上分配相同的角色

谢谢
英文:

Does anybody know why this python script is so slow?

It starts to become slow when I start to call the await member.remove_roles() and await member.add_roles(). When I comment the await member.remove_roles() and await member.add_roles() then the performance seems to look ok.

Some background what I want to accomplish.
User subscribe via a website. This data is stored into a mysql DB. Based on the subscription they got a user role assigned.
Base on that user role I would like to assign the same role on a Discord server.

Thx

import asyncio
import discord
import mysql.connector
import os
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

# Discord bot token
BOT_TOKEN = os.getenv("DISCORD_TOKEN")
DISCORD_SERVER_ID = os.getenv("DISCORD_SERVER_ID")


# Connect to the MySQL database
db_host = os.getenv("DB_HOST")
db_user = os.getenv("DB_USER")
db_password = os.getenv("DB_PASSWORD")
db_name = os.getenv("DB_NAME")

db_connection = mysql.connector.connect(
    host=db_host,
    user=db_user,
    password=db_password,
    database=db_name
)
db_cursor = db_connection.cursor()

# Create a Discord client
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)

async def updateActiveMemberRoles():
    print('def executing')
    await client.wait_until_ready()
    # Get all the members in the guild
    guild = client.get_guild(913107079605141525)  # Replace with the ID of your Discord guild (SERVER ID)

    while not client.is_closed():
        
        ROLE_GOLD    = guild.get_role(991694541298090059)
        ROLE_BRONZE  = guild.get_role(914277751719096381)
        ROLE_VISITOR = guild.get_role(914280052424859700)

        for member in guild.members:
            DiscordName = member.name+"#"+member.discriminator
            print(DiscordName)

            db_cursor.execute("select membership_level from vw_discord_active_members where usrAccount = %s", (DiscordName,))
            result = db_cursor.fetchone()

            if result:
                print(DiscordName+" "+result[0])
            
                membership_level = result[0]
                if membership_level == "gold":
                    await member.remove_roles(ROLE_VISITOR)
                    await member.add_roles(ROLE_BRONZE)
                    await member.add_roles(ROLE_GOLD)
                    print("gold")
                elif membership_level == "bronze":
                    await member.remove_roles(ROLE_VISITOR)
                    await member.remove_roles(ROLE_GOLD)
                    await member.add_roles(ROLE_BRONZE)
                    print("bronze")
            else: 
                await member.remove_roles(ROLE_BRONZE)
                await member.remove_roles(ROLE_GOLD)
                await member.add_roles(ROLE_VISITOR)
                print("visitor")
                
        print("------------------------------")
        print("--- Wait 60 Seconds ----------")
        print("------------------------------")
        await asyncio.sleep(60)


@client.event
async def on_ready():
    print("Bot is ready.")
    client.loop.create_task(updateActiveMemberRoles())


client.run(BOT_TOKEN)

答案1

得分: 1

看起来你正在尝试迭代服务器中的每个成员,然后从在线数据库获取数据,而你的程序必须通过互联网连接到该数据库。根据你的互联网连接质量如何,这可能会变得很慢,当你必须为服务器上的每个成员执行此操作时,可能需要一些时间。

我相当肯定每个角色都有一个包含该角色成员的列表,所以最好的方法可能是只使用role.members的for循环,然后移除包含该角色的成员。

以下是如何在文档中使用它

英文:

It looks like you’re trying to iterate through every single member in the server, then fetch data from an online db that your program has to connect to over the internet. Depending on how good your internet connection is, this could become slow and when you have to do this for every single member on the server it could take some time.

Im pretty sure each role has a list of members with that role so the best way to go about it would be to probably just use a for loop for role.members then remove members that contain that role.

Here’s how you use it in the documentation.

huangapple
  • 本文由 发表于 2023年5月29日 03:39:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定