繁体   English   中英

为什么列表附加在 discord.py 中不起作用?

[英]why are list appends not working in 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')

dsakfjasdfkajsdfikcmedksnjedkjnfskdjnfcjsdjkfkszidfnzsjkeudfnhszjwedfhnzsjkdefnkzsdxjfnzswdxfnwxnzksxdjnfwszexznswexnjdsnhjsdn

如果要追加到列表中,请在函数外分配初始值,然后在函数内追加即可。

如果分配给函数中的列表,则所有先前的元素都将被丢弃。

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM