Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - Discord py - server list in multiple messages

I have a $allservers command in my discord bot and reached the text limit from 2.000 characters. So I'm asking, is it possible to get the server list from a MySQL database and put them into multiple messages?

Example:

FIRST MESSAGE: server1, server2, server3
-- REACHED TEXT LIMIT, NEXT MESSAGE
SECOND MESSAGE: server4, server5, server6

My code for the $allservers command:

@bot.command(aliases=["servers"])
async def allservers(ctx):
    mydb = mysql.connector.connect(
        host="**",
        user="**",
        password="**",
        database="**"
    )

    mycursor = mydb.cursor()
    guild56 = bot.get_guild(616655040614236160)
    member = guild56.get_member(ctx.author.id)
    role2 = guild56.get_role(792894127972155393)  # Admin
    role3 = guild56.get_role(792894172829974529)  # Mod

    if role2 in member.roles or role3 in member.roles:
        mycursor.execute(f"SELECT * FROM servers")
        myresult = mycursor.fetchall()
        message = ''
        for x in myresult:
            try:
                x1 = [x]
                guild = bot.get_guild(int(x1[0][0]))
                channel = bot.get_channel(int(x1[0][1]))
                message += f'**{guild}** - `{channel.name}` - `{guild.id}`
'
            except:
                pass

        await ctx.author.send(f"??** | ALLE SERVERS**

{message}")
        await ctx.message.delete()

My bot is on many servers, that's why the bot reached the discord message text limit from 2.000 characters.

question from:https://stackoverflow.com/questions/65644187/discord-py-server-list-in-multiple-messages

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

1 Reply

0 votes
by (71.8m points)

You could solve this by changing message = '' as a string to a list messages = [] and checking every loop if your message is still within the character limit. (and updating the rest of the funtion to work with that)

Like this:

if role2 in member.roles or role3 in member.roles:
        mycursor.execute(f"SELECT * FROM servers")
        myresult = mycursor.fetchall()
        messages = ['']
        counter = 0
        for x in myresult:
            if len(messages[counter]) > 970:
                messages.append('')
                counter += 1
            try:
                x1 = [x]
                guild = bot.get_guild(int(x1[0][0]))
                channel = bot.get_channel(int(x1[0][1]))
                messages[counter] += f'**{guild}** - `{channel.name}` - `{guild.id}`
'
            except:
                pass
        await ctx.author.send(f"??** | ALLE SERVERS**

{messages[0]}")
        for i in range(1, len(messages)):
                await ctx.author.send(messages[i])
        await ctx.message.delete()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...