1 year ago

#75021

test-img

Rectangle Man

My Bot Fails To Execute Commands When This Event Decorator is Active

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'])

python

json

discord

discord.py

bots

0 Answers

Your Answer

Accepted video resources