当命令在多个不同的服务器上处于活动状态时,如何在discord.py上使用while循环

2024-09-20 17:51:27 发布

您现在位置:Python中文网/ 问答频道 /正文

所以问题是,我有一个discord.py机器人,它会滥发信息,所以我显然需要一个while循环现在的问题是,当我在几个不同的服务器上运行代码时,它工作正常,但当我尝试停止它时,它会停止在它以前工作正常的每台服务器上滥发信息。我已经穷途末路了,任何帮助都将不胜感激

这是我到目前为止想出的代码

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

thislist = []
s = None
ga = None

@client.event
async def on_message(message):
    if message.content == ".stopbot":
        ad = message.guild.id
        print('dayum')
        global ga
        ga = True
    if message.author == client.user:
        return
    if message.content.startswith('.spam'):
        global thislist
        r = message.guild.id
        thislist.append(r)
        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 ga == True:
           print("shit")
           for word in thislist:
               print(word)
               print(s)
               if str(word) == str(ad):
                   print('lol')
                   return
        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 word in thislist:
        print(word)
        print(s)
        if str(word) == str(s):
            global z
            z = 1



client.run('<Token>')

抱歉,如果代码有点混乱,我们尝试了很多不同的东西,所以它不是特别干净。谢谢


热门问题