Created
December 28, 2020 08:39
-
-
Save AnshAg2007/d7ffe1d3f22806d118bb9fff282a75ac to your computer and use it in GitHub Desktop.
Basic Bot Setup With Cogs & Custom Prefix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""The Creator of this code is AnshAg2007""" | |
"""This is a very basic setup of a discord.py bot with cogs and custom prefix | |
But to use it,you must know the basic cog structures too.""" | |
import discord | |
import json | |
import os | |
from discord.ext import commands | |
"""At Above we have imported discord.py,os and json database, | |
we also imported commands from discord extension.""" | |
"""Here Comes Our Custom Prefix Part, | |
You will need a prefixes.json file to store the prefixes for every server""" | |
def get_prefix(bot,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) | |
"""Here we will make a cogs loader/unloader command, | |
And will do a basic eval command import called Jishaku/Jsk""" | |
bot.load_extension('jishaku') | |
@bot.command(hidden=True) | |
@commands.is_owner() | |
async def load(ctx,extension,hidden=True): | |
bot.load_extension(f'cogs.{extension}') | |
@bot.command(hidden=True) | |
@commands.is_owner() | |
async def unload(ctx,extension): | |
bot.unload_extension(f'cogs.{extension}') | |
for filename in os.listdir('./cogs'): | |
if filename.endswith('.py'): | |
bot.load_extension(f'cogs.{filename[:-3]}') | |
@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(brief=f'change bot\'s prefix') | |
@commands.has_guild_permissions(manage_guild=True) | |
async def prefix(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 changed to {prefix}') | |
"""Here you need your bot's token from Developer Portal""" | |
bot.run('bot token') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Make Sure to paste
{}
inprefixes.json
,if it says ignored error in console,then kick and re-add the bot in the server.