0

My script for the bot works individually. My event decorator block works individually. But somehow when they work together, my commands don't run.

What I am trying to do here is that I want to display the current ongoing bot's prefix of the server whenever "gravy get prefix" is written. I can't do this by a command decorator as it would require the prefix and if someone's new to the server, he/she may not know the prefix if the default prefix was changed. So, I created an event decorator which basically displays the prefix from my .json file (prefix.json). After doing so, the bot returns the prefix whenever "gravy get prefix" is written BUT it DOES NOT run any command.

Pls help me fix this or tell any other alternative through which I can resolve my issue.

Thanks in advance

Here's my code:

#MODULES
import discord
import os
from discord.ext import commands
from keep_alive import keep_alive
import random
import json


#PREFIX

def get_prefix(client,message):
  with open("prefix.json","r") as f:
    prefix=json.load(f)


  return prefix[str(message.guild.id)]


  
client = commands.Bot(command_prefix = get_prefix)

@client.event
async def on_guild_join(guild):
  with open("prefix.json","r") as f:
    prefix=json.load(f)

  prefix[str(guild.id)]=")"

  with open("prefix.json","w") as f:
    json.dump(prefix,f,indent=4)

@client.event
async def on_guild_remove(guild):
  with open("prefix.json","r") as f:
    prefix=json.load(f)

  prefix.pop(str(guild.id))

  with open("prefix.json","w") as f:
    json.dump(prefix,f,indent=4)

@client.command(aliases=["cp"])
async def changeprefix(ctx, prefix):
  with open("prefix.json","r") as f:
    prefixes=json.load(f)

  prefixes[str(ctx.guild.id)]=prefix

  with open("prefix.json","w") as f:
    json.dump(prefixes,f,indent=4)

  await ctx.send(f"prefix changed to: {prefix}")





@client.event
async def on_message(message):
  if message.author==client.user:
    return 
    
  if message.content.startswith("gravy get prefix"):
    with open("prefix.json","r") as f:
      prefixes=json.load(f)

      await message.channel.send(prefixes[str(message.guild.id)])

#COMMANDS
@client.command(aliases=["allcap","allup","capitalise","caps"])
async def allupcase(ctx,*text):
  s=""
  for i in text:
    for j in i:
      s+=j.upper()
    s+=" "
    
  await ctx.send(s)


client.run(os.environ['TOKEN'])
  • 4
    Does this answer your question? [Why does on\_message stop commands from working?](https://stackoverflow.com/questions/49331096/why-does-on-message-stop-commands-from-working) – Łukasz Kwieciński Jan 20 '22 at 13:53
  • i think it has got something to do with the on message command so your first command in the on message states if message.author==client.user: return that mean the bot user message there will not be any response try removing that statement and try again. – majinvejetho Jan 20 '22 at 14:43
  • @majinvejetho The for the respose! It was somewhat like that. Basically I fixed it by executing a statement which allows the commands under the command decorator to execute even in the presence of the event decorator which executes every time a user sends a message. – Rectangle Man Feb 04 '22 at 18:15

0 Answers0