Cake
Cake

Reputation: 11

How do I add commands that allow me to start and stop a discord bot without completely ending its process (python)

I am trying to create a discord bot, the purpose of the bot is to output a specific string when a specific word is detected.

I have already figured out how to do that and it works just fine, but what I am really struggling with is how to give the bot start and stop commands that would look like this: !start, !stop. I also wanted to output a message when each command is triggered in order to let users know that it has worked.

any help is appreciated and thank you in advance.

import os
import discord
from keep_on import keep_on
bot_token = os.environ['TOKEN']

client = discord.Client()

@client.event
async def on_ready():
  print('{0.user} is online'.format(client))

@client.event
async def on_message(message):
  sentWord = message.content
  CSentWord = sentWord.upper()
  if message.author == client.user:
    return
  if "SORRY" in CSentWord:
    await message.channel.send("'We're Very Sorry' - Joey Tribbiani")
    await message.channel.send(file=discord.File('Joey Is Sorry.png'))
  if "S_O_R_R_Y" in CSentWord:
    await message.channel.send("'We're Very Sorry' - Joey Tribbiani")
    await message.channel.send(file=discord.File('Joey Is Sorry.png'))
  if "S.O.R.R.Y" in CSentWord:
    await message.channel.send("'We're Very Sorry' - Joey Tribbiani")
    await message.channel.send(file=discord.File('Joey Is Sorry.png'))
  if "S|O|R|R|Y" in CSentWord:
    await message.channel.send("'We're Very Sorry' - Joey Tribbiani")
    await message.channel.send(file=discord.File('Joey Is Sorry.png'))

keep_on()
client.run(os.getenv('TOKEN'))

Upvotes: 0

Views: 660

Answers (2)

Max Shouman
Max Shouman

Reputation: 1331

You would have to add some sort of global check, and you have many options to do it. E.g, you could handle the check in a on_command event, or even at the bot level with a bot.check decorator.

Then you would implement a command with the appropriate permissions that allows you to start/stop the bot activity. For example I'd enable only myself to run the control commands.

Here's a nice way I would design the idea:

from discord.ext import commands

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

is_active = True

@bot.check
async def isactive(ctx):
    if not is_active:
        await ctx.send(embed=a_nice_embed_showing_inactive_status)
        return
    return True

# the following essentially creates your own decorator to wrap control commands like start and stop.
def isme():
    def control_command(ctx):
        return ctx.author.id == my_id
    return commands.check(control_command)

@bot.command()
@isme()
async def stop(ctx):
    if not is_active:
        await ctx.send("The bot is already active!")
    else:
        is_active = False
        await ctx.send("The bot is active now.")

@bot.command()
@isme
async def start(ctx):
    # exact opposite of stop

@bot.command()
async def random_command(ctx):
    # this will fail if not is_active

bot.run("token")

Upvotes: 0

Krafpy
Krafpy

Reputation: 346

A simple way to do this would be to have a (global) boolean variable enabled which is toggled to True when receiving the command !start, and to False on the !stop command. Then when checking for other commands than !start and !stop, first check the value of enabled and execute the commands only if enabled is True. Sending a message when receiving one of these two commands is also straightforward.

This can look like something like this:

enabled = False

# Method called when the bot receives a message
async def on_message(message):
    global enabled
    
    if message.content == "!start":
        enabled = True
        await message.channel.send("Bot is on.")
    elif message.content == "!stop":
        enabled = False
        await message.channel.send("Bot is off.")
    elif enabled:
        # Do whatever is done when the bot receives a message normally
        # ...

Though using global is a known bad practice, but this would work as a first approach.

Upvotes: 1

Related Questions