Home > Blockchain >  How can i make a toggle on and off command for this command?
How can i make a toggle on and off command for this command?

Time:05-08

So im trying to make an anti-link/anti-swear discord bot but idk how to make a toggle command. This is the code that i have made so far.

import discord


client = discord.Client()
key = "Secret"


block_words = ["http://", "https://"]


@client.event
async def on_ready():
    print(f"Bot logged in as {client.user}") 

# The on_message event happens when a message gets sent on the server
@client.event
async def on_message(msg):


    if msg.author != client.user:

        for text in block_words:

                await msg.delete()
                return 


        print("Not Deleting...")
        

client.run(key)

If anyone can make it please make it i'd appriciate it :)

CodePudding user response:

Make a json file or use a database to store if the server wants it enabled or disabled.

import discord
import json
from discord.ext import commands


client = discord.Client()
key = "Secret"


block_words = ["http://", "https://"]


@client.event
async def on_ready():
    print(f"Bot logged in as {client.user}") 

# The on_message event happens when a message gets sent on the server
@client.event
async def on_message(msg):
   with open('automod.json', 'r') as f:
      data = json.load(f)

    if msg.author != client.user and data[msg.guild.id]:  # Filter words only if enabled

        for text in block_words:
           if text in msg.content:  # Check if blocked words are there in the message content
                await msg.delete()
                return 


        print("Not Deleting...")

# Make a command to configure the automod
@client.command(name='put_name_here')
@commands.has_permissions(manage_guild=True)
async def auto_mod_config(ctx):
   with open('automod.json', 'r') as f:
      data = json.load(f)
      data[ctx.guild.id] = not data[ctx.guild.id]  # Toggle happens here
   
   with open('automod.json', 'w') as f:
      json.dump(data, f)

client.run(key)

This is how automod.json should look:

{
   guild_id(the id of the server): true or false
}

CodePudding user response:

If I understand you correctly, you just need to add an if statement which checks if toggle variable is true. (I've never worked with discord.js so I can't be sure if the code is correct.)

import discord

client = discord.Client()
key = "Secret"


block_words = ["http://", "https://"]
toggle = True

@client.event
async def on_ready():
    print(f"Bot logged in as {client.user}") 

# The on_message event happens when a   
message gets sent on the server
@client.event
async def on_message(msg):

    if msg.author != client.user AND toggle == True:

        for text in block_words:

                await msg.delete()
                return 


        print("Not Deleting...")
    if "!toggle" in message.content:
        toggle = !toggle
        await message.channel.send("Is anti-link enabled: " toggle)


client.run(key)
  • Related