Home python Change command prefix discord.py

Change command prefix discord.py

Author

Date

Category

How can I change the bot through prefix command in discord.py? I tried to use this code, but says that is not found setPrefix team, understand that it comes after bot.run but the code was. If you put it in front of bot.run and changing @ commands.command on @ bot.command it gives an error str object has no attribute guild. Also I do not understand why in the second argument variable bot written three points. Help please.

import discord
from discord.ext import commands
custom_prefixes = {}
default_prefixes = [ '.']
async def determine_prefix (bot, message):
  guild = message.guild
  if guild:
    return custom_prefixes.get (guild.id, default_prefixes)
  ELSE:
    return default_prefixes
bot = commands.Bot (command_prefix = determine_prefix, ...)
bot.run ( 'token')
@ Commands.command ()
@ Commands.guild_only ()
async def setprefix (self, ctx, *, prefixes = ""):
  custom_prefixes [ctx.guild.id] = prefixes.split () or default_prefixes
  await ctx.send ( "Prefixes set!")

Answer 1, Authority 100%

Why do not you make through the database, it is more convenient in the future if your bot is on many servers that list does not cope with the volume of information

And read documentation please

PostgreSQL

First, when the boat comes to the server in the database be entered credit default prefix

@ commands.Cog.listener ()
async def on_guild_join (self, guild):
  cursor.execute (. f'INSERT INTO public "prefixDB" (guild_id, prefix) VALUES ({Haydee server to yakogo joins bot} \ 'credit default prefix \') ')
  conn.commit ()

Also, when the boat comes with the server, we remove the server from the database

@ commands.Cog.listener ()
async def on_guild_remove (self, guild):
  cursor.execute (. f'DELETE FROM public "prefixDB" WHERE guild_id = {Haydee server disconnects from yakogo bot} ')
  conn.commit ()

Here you are entering perfiks server to the database.

@ commands.command ()
@ Commands.check (is_owner_guild)
async def prefix (self, ctx, prefix):
  guildid = ctx.guild.id
  cursor.execute (f'UPDATE public "prefixDB" SET prefix = \ '{prefix} \' WHERE guild_id = \ '{guildid} \';. ') # here you are upgrading the server prefix
  conn.commit ()
  emb = discord.Embed (title = 'Succeeded!', description = f'Prefiks server changes to "** {prefix} **" ', colour = discord.Color.green (), timestamp = ctx.message.created_at )
  emb.set_footer (text = ctx.message.author)
  await ctx.send (embed = emb)
@ prefix.error
async def prefix_error (self, ctx, error):
  if isinstance (error, commands.CheckFailure):
    emb = discord.Embed (timestamp = ctx.message.created_at, title = 'Error !!!', colour = discord.Color.red (), description = 'This command can only use the server owner')
    emb.set_footer (text = ctx.message.author)
    await ctx.channel.purge (limit = 1)
    await ctx.send (embed = emb)

After just over function takes it:

def get_prefix (bot, message):
  guildid = message.guild.id # Haydee server where the command was vizvat prefix
  cursor.execute (f'SELECT prefix FROM public "prefixDB" WHERE guild_id = \ '{guildid} \';. ')
  prefix = cursor.fetchone ()
  conn.commit ()
  return prefix
bot = commands.Bot (command_prefix = get_prefix, help_command = None)

Answer 2

Try by such commands:

# Use the same prefix
bot = commands.Bot (command_prefix = '$') # $ - prefix
# If you used the default prefix or prefix!
bot = commands.Bot (command_prefix = commands.when_mentioned_or ( "!")) #! - prefix

Answer 3

I do not know how correctly you need to do it, but I had a crutch like changing the value Command_Prefix Bot in the On_Message :

async def on_message (message):
   prefix = ... # here you get a bot prefix on the server
   Bot.comMand_Prefix = Prefix.
   Await Bot.Process_Commands (Message)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions