Night Fury
Night Fury

Reputation: 29

why are list appends not working in discord.py?

This code should be working without any problems, but it registers the first server i type .spam on and if i run it on another server it doesn't append the list but instead changes the first server id to the last one

async def on_message(message):
    if message.author == client.user:
        return
    if message.content.startswith('.spam'):
        global thislist
        thislist = [1, 3]
        thislist.append(message.guild.id)
        print(thislist)
        print(len(thislist))
    if message.content.startswith('.stop'):
        global s
        s = str(message.guild.id)
    await client.process_commands(message)

any help would be appreciated

The complete code:

import discord, pyautogui, time
from discord.ext import commands


client = commands.Bot(command_prefix = '.')

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content.startswith('.spam'):
        global thislist
        thislist = [message.guild.id]
        print(thislist)
        print(len(thislist))
    if message.content.startswith('.stop'):
        global s
        s = str(message.guild.id)
    await client.process_commands(message)

@client.command()
@commands.has_role('discordpy')
async def spam(ctx, *, my_id):
    print("it worked")
    time.sleep(5)
    x = 1
    y = 0
    global z
    z = 0


    while y != 1:
        if z == 1:
            z = 0
            break
        myid = str(my_id)
        await ctx.channel.send(myid + " x " +str(x))
        print(x)
        x += 1
        print(y)
        time.sleep(0.5)

@client.command()
@commands.has_role('discordpy')
async def stop(ctx):
    for x in thislist:
        print(thislist)
        if x == s:
            global z
            z = 1



@client.command()
@commands.has_role('discordpy')
async def stopbot(ctx):
    self.isrunning = False






client.run('token')

dsakfjasdfkajsdfikcmedksnjedkjnfskdjnfcjsdjkfkszidfnzsjkeudfnhszjwedfhnzsjkdefnkzsdxjfnzswdxfnwxnzksxdjnfwszexznswexnjdsnhjsdn

Upvotes: 0

Views: 165

Answers (1)

Barmar
Barmar

Reputation: 782130

If you want to append to the list, assign the initial value outside the function, and then just append in the function.

If you assign to the list in the function, all the previous elements are discarded.

thislist = [1, 3]

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content.startswith('.spam'):
        thislist.append(message.guild.id)
        print(thislist)
        print(len(thislist))
    if message.content.startswith('.stop'):
        global s
        s = str(message.guild.id)
    await client.process_commands(message)

Upvotes: 1

Related Questions