1

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}`\n'
            except:
                pass

        await ctx.author.send(f"** | ALLE SERVERS**\n\n{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.

  • Do you really connect to the database each time you run the command? Also small suggestion - you shouldn't use mysql unless you really don't have any other option – Łukasz Kwieciński Jan 09 '21 at 15:18
  • Instead of `message += f'**{guild}** - {channel.name} - {guild.id}\n'`, why don't you just do `await ctx.author.send(f'**{guild}** - {channel.name} - {guild.id}')`? You'll get `n` different messages for `n` different servers sent to you in your DMs. Also, you don't really need a database to get all the guilds that your bot is in. That aside, would this answer your question: https://stackoverflow.com/q/62878525/11146632 ? – Sujit Jan 09 '21 at 15:19
  • No, I need a function to do it with the database. Sadly I don't know which saver I should use, so I use MySQL. And it's not just the bot guilds, they must activate something in their server to be in this list. –  Jan 09 '21 at 15:43
  • alright, and what's wrong with sending separate messages for each guild like i said above? – Sujit Jan 09 '21 at 15:47

1 Answers1

0

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}`\n'
            except:
                pass
        await ctx.author.send(f"** | ALLE SERVERS**\n\n{messages[0]}")
        for i in range(1, len(messages)):
                await ctx.author.send(messages[i])
        await ctx.message.delete()
FierySpectre
  • 642
  • 3
  • 9
  • i got the error IndexError: list index out of range for `if len(messages[counter]) > 970:` –  Jan 11 '21 at 19:27
  • 1
    Yeah, that is because I totaly forgot to put the first value in the list (it was trying to check the first value of an empty list), it's fixed now – FierySpectre Jan 12 '21 at 20:55