简体   繁体   中英

how to make a discord bot stop spamming when it is spamming in discord py

how to make a discord bot stop spamming when it is spamming in discord py ( the same bot )?

I have made my discord bot can spam, but I can't stop the bot from spamming when it is already spamming... how do you do it? can anyone help me? Thx in advance!

@bot.command(name="spam", help="spam some message")
async def spam(ctx, times:int, *, message):
  for i in range(times):
    await ctx.send(message)

Maybe you can use global flag variable which can be set by another command (called below as stopspam yes ) to stop the spam messages.

Please find the full implementation below,

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix='!')

@bot.event
async def on_ready():
    # create the global spam stop flag
    # and set False
    global stop_spam_flag
    stop_spam_flag = False
    print(f'Logged in as {bot.user}')

@bot.command(name="spam", help="spam some message")
async def spam(ctx, times:int, *, message):
    # get the global flag into this function
    global stop_spam_flag
    for i in range(times):
        await ctx.send(message)
        # check the global flag here
        if stop_spam_flag:
            break
    # reset the flag
    stop_spam_flag = False

@bot.command(name="stopspam", help="stops the spam messages")
async def stopspam(ctx, *, message):
    # get the global flag
    global stop_spam_flag
    if message == 'yes':
        # set the global flag
        stop_spam_flag = True

bot.run('TOKEN HERE!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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