为什么list appends在discord.py中不起作用



这段代码应该可以正常工作,但它注册了我键入的第一个服务器.spam,如果我在另一台服务器上运行它,它不会附加列表,而是将第一个服务器id更改为最后一个

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)

如有任何帮助,将不胜感激

完整代码:

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')

dsakfjasdfkajsdfikcmedksnjedkjnfskdjnfcjsdjkfkszidfnzjkeudfnhszjwedfhnsjkdefnkzsdxjfnzswdxfnwxnzksxdjnfwszexznswexnjdsnhjsdn

如果要追加到列表中,请在函数外指定初始值,然后只追加到函数中。

如果指定给函数中的列表,则会丢弃以前的所有元素。

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)

最新更新