0
votes

I wanted my bot to send the prefix of that particular server when it pinged (mentioned).

I did google it but it didn't work (Discord.py- How to make a bot message when its pinged?)

it stops from other commands being executed, etc.

this is my code

import random
import json
from discord.ext import commands
import numpy
from urllib.request import urlopen as url
from discord.ext.commands import cooldown, BucketType
#testbot
def get_prefix(client, message):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(f)
    
    return prefixes[str(message.guild.id)]

bot = commands.Bot(command_prefix = get_prefix)

@bot.event
async def on_ready():
    print(f'bots up')

@bot.event
async def on_guild_join(guild):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(f)
    
    prefixes[str(guild.id)] = '.'
    with open('prefixes.json', 'w') as f:
        json.dump(prefixes, f, indent=4)

@bot.event
async def on_guild_remove(guild):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(f)
    
    prefixes.pop(str(guild.id))
    with open('prefixes.json', 'w') as f:
        json.dump(prefixes, f, indent=4)

@bot.command()
async def setprefix(ctx, prefix):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(f)
    
    prefixes[str(ctx.guild.id)] = prefix
    with open('prefixes.json', 'w') as f:
        json.dump(prefixes, f, indent=4)
    await ctx.send(f'prefix set to: {prefix}')

@bot.command(description='check bot ping type .ping')
async def ping(ctx):
    await ctx.send(f'{round(bot.latency*1000)}ms')
@bot.event
async def on_message(message):
    if bot.user.mentioned_in(message):
        print("pinged")
        await message.channel.send('My prefix here is {0} '.format(bot.command_prefix))


bot.run('token')

sends me this message instead

<function get_prefix at 0xb55a16a8>

1

1 Answers

1
votes

You can use on_message event to check when your bot is mentioned.

@bot.event
async def on_message(message):
    if bot.user.mentioned_in(message) and message.mention_everyone is False:
        prefix= get_prefix
        await message.channel.send(f"My prefix is {bot.command_prefix}")

     await bot.process_commands(message) # This line makes your other commands work.

The last line is probably your solution to the commands stopping to work after using on_message event.