-1
# Imports
import discord
import random
import os
from discord.ext import commands
from keep_alive import keep_alive

# Credentials
TOKEN = os.environ['Token']

# Create bot
client = commands.Bot(command_prefix='Aimby')

badword = ["word1","word2","word3"]
censorprotocol = False

# Startup Information
@client.event
async def on_ready():
  print('Starting bot: {}'.format(client.user.name))
  print('Status: Online')

@client.event
async def on_message(message):
#Aimby censoring
  censorprotocol = False   
  if message.content.startswith('Aimby initiate censor protocol'):
    if censorprotocol == False:
      await message.channel.send("Censoring bad words.")
      censorprotocol = True
    else:
      await message.channel.send("Already censoring bad words.")

  if message.content.startswith('Aimby complete censor protocol'):
    if censorprotocol == True:
      await message.channel.send("Stopped censoring bad words.")
      censorprotocol = False
    else:
      await message.channel.send("Currently not censoring bad words.")
    
  while censorprotocol == True:
    if censorprotocol == True and any(word in message.content.lower() for word in badword):
      await message.delete()
      await message.channel.send("Language!")
    else:
      return

keep_alive()
client.run(TOKEN)

So I want to create a boolean variable that would normally be false. When I say "Aimby initiate censor protocol" it should make the variable true, thus also triggering the while loop. But for some reason, the "censorprotocol" variable does not change states (true or false) and the bot doesn't read the text in the while loop (plus I don't think it works).

  • u don't need to have a while loop inside the on_message function, just the if condition and the if block code is enough, and better use `@client.command()` stuff instead of if/else statement for each command in on_message function – Ghost Ops Dec 17 '21 at 13:16
  • You set `censorprotocol` to `False` every time `on_message` is invoked. Why? This will effectively disable your censoring protocol whenever anyone sends a message in the channel. – esqew Dec 17 '21 at 13:16
  • @esqew Oh ok, I got confused and did not use `global censorprotocol` in the function. But now I got it to correctly identify the boolean variable. – Chiratt Thames Dec 17 '21 at 13:33
  • @GhostOps Can you clarify how I should use `@client.command()`? – Chiratt Thames Dec 17 '21 at 13:34
  • @ChirattThames i posted about it as an answer – Ghost Ops Dec 17 '21 at 13:46

1 Answers1

1

You don't need to have a while loop inside the on_message function, just the if condition and the if block code is enough, and better use @client.command() stuff instead of if/else statement for each command in on_message function

And as esqew said, You set censorprotocol to False every time on_message is invoked, which will disable your censoring protocol whenever anyone sends a message

Try this code

# Imports
import discord
import random
import os
from discord.ext import commands
from keep_alive import keep_alive

# Credentials
TOKEN = os.environ['Token']

# Create bot
client = commands.Bot(command_prefix='Aimby', strip_after_prefix=True)

badword = ["word1","word2","word3"]
censorprotocol = False

# Startup Information
@client.event
async def on_ready():
  print('Starting bot: {}'.format(client.user.name))
  print('Status: Online')

@client.listen('on_message')
async def on_message_function(message):
  global censorprotocol
    
  if censorprotocol and any(word in message.content.lower() for word in badword):
    await message.channel.send("Language!")
    await message.delete()
  else:
    return

@client.command(name='icp')
async def initiate_censor_protocol(ctx):
  global censorprotocol
  if not censorprotocol:
    await ctx.send("Censoring bad words.")
    censorprotocol = True
  else:
    await ctx.send("Already censoring bad words.")

@client.command(name='ccp')
async def complete_censor_protocol(ctx):
  global censorprotocol
  if censorprotocol:
    await ctx.send("Stopped censoring bad words.")
    censorprotocol = False
  else:
    await ctx.send("Currently not censoring bad words.")

keep_alive()
client.run(TOKEN)

By this code, you would use the commands like Aimby icp for initiating the protocol, and Aimby ccp for stopping the protocol (there can be no spaces in the command names, so I shortened it like that)

Or you can change whatever u want in the name parameter of the @client.command() decorator

Tell me if its not working...

Ghost Ops
  • 1,710
  • 2
  • 13
  • 23
  • Hmmm. Thanks for the help, but it doesn't seem to do anything. No commands were detected (I tested by using print("something") and nothing happened on discord too. Edit: I messed around with some of your code and added to mine and it works now. Thanks again :) – Chiratt Thames Dec 17 '21 at 13:56
  • 1
    Don't forget that when using an `on_message` event you should [process](https://stackoverflow.com/a/49331419/14420546) the event so commands work as well. – Bagle Dec 17 '21 at 14:56