againzeenox
againzeenox

Reputation: 1

How to stop a while loop in Discord.py

I have made a Discord Bot that pings members(spam Bot). It uses a while loop. It automatically stops in a few hours(IDK Why But probably because the host I use resets the connection after a few hours). I wanna make a command so that the bot stops pinging members but still be online. Basically I wanna end the while loop I use. Can anyone help me?

import discord
from keep_alive import keep_alive

client = discord.Client()
roleid = os.environ["@PingersRoleID"]
CategoryID = os.environ["PingsCategoryID"]
PingChannels = [
    "ping1",
    "ping2",
    "ping3",
    "ping4",
    "ping5",
    "ping6",
    "ping7",
    "ping8",
    "ping9",
    "ping10",
]
PingBotToken = os.environ["PingBotToken"]


@client.event
async def on_ready():
    print("{0.user} has joined the chat".format(client))


@client.event
async def on_message(message):
    username = str(message.author).split('#')[0]
    user_message = str(message.content)
    channel = str(message.channel.name)
    # guild = client.get_guild(CategoryID)
    print("{} sent {} in {}".format(username, user_message, channel))
    if user_message.lower() == "!activatepings":
      for _ in PingChannels:
          if message.channel.name == str(_):
              while True:
                  await message.channel.send("<@&{}>".format(roleid))


keep_alive()
client.run("...")

Upvotes: 0

Views: 125

Answers (2)

Tony
Tony

Reputation: 326

You can use a task that can be started and canceled at any time:

from discord.ext import tasks

@client.tasks(seconds=3600) #set to any amount
async def ping_task():
     for channel in client.get_all_channels():
         if channel.name in PingChannels:
             await channel.send("<@&{}>".format(roleid))

@client.event
async def on_message(message):
    username = str(message.author).split('#')[0]
    user_message = str(message.content)
    channel = str(message.channel.name)
    # guild = client.get_guild(CategoryID)
    print("{} sent {} in {}".format(username, user_message, channel))
    if user_message.lower() == "!activatepings":
        for _ in PingChannels:
            if message.channel.name == str(_):
                ping_task.start() #The task starts here

@client.command()
async def someCommand(ctx):
    ping_task.cancel() #The task ends with this command

You can find an example of how to use tasks here: https://github.com/Rapptz/discord.py/blob/v2.0.0/examples/background_task.py

Upvotes: 1

filipporomani
filipporomani

Reputation: 289

@client.event
async def on_message(message):
    username = str(message.author).split('#')[0]
    user_message = str(message.content)
    channel = str(message.channel.name)
    # guild = client.get_guild(CategoryID)
    print("{} sent {} in {}".format(username, user_message, channel))
    if user_message.lower() == "!activatepings":
      for _ in PingChannels:
          if message.channel.name == str(_):
              while True:
                  if message.content == "STOP": ## replace it with the command you want to stop the bot
                      break
                  else:
                      await message.channel.send("<@&{}>".format(roleid))

Upvotes: 0

Related Questions