Skip to content

Instantly share code, notes, and snippets.

@EvieePy
Last active April 24, 2024 09:30
Show Gist options
  • Save EvieePy/d78c061a4798ae81be9825468fe146be to your computer and use it in GitHub Desktop.
Save EvieePy/d78c061a4798ae81be9825468fe146be to your computer and use it in GitHub Desktop.
A Cogs Example for the rewrite version of - discord.py
import discord
from discord.ext import commands
import sys, traceback
"""This is a multi file example showcasing many features of the command extension and the use of cogs.
These are examples only and are not intended to be used as a fully functioning bot. Rather they should give you a basic
understanding and platform for creating your own bot.
These examples make use of Python 3.6.2 and the rewrite version on the lib.
For examples on cogs for the async version:
https://gist.github.com/leovoel/46cd89ed6a8f41fd09c5
Rewrite Documentation:
http://discordpy.readthedocs.io/en/rewrite/api.html
Rewrite Commands Documentation:
http://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html
Familiarising yourself with the documentation will greatly help you in creating your bot and using cogs.
"""
def get_prefix(bot, message):
"""A callable Prefix for our bot. This could be edited to allow per server prefixes."""
# Notice how you can use spaces in prefixes. Try to keep them simple though.
prefixes = ['>?', 'lol ', '!?']
# Check to see if we are outside of a guild. e.g DM's etc.
if not message.guild:
# Only allow ? to be used in DMs
return '?'
# If we are in a guild, we allow for the user to mention us or use any of the prefixes in our list.
return commands.when_mentioned_or(*prefixes)(bot, message)
# Below cogs represents our folder our cogs are in. Following is the file name. So 'meme.py' in cogs, would be cogs.meme
# Think of it like a dot path import
initial_extensions = ['cogs.simple',
'cogs.members',
'cogs.owner']
bot = commands.Bot(command_prefix=get_prefix, description='A Rewrite Cog Example')
# Here we load our extensions(cogs) listed above in [initial_extensions].
if __name__ == '__main__':
for extension in initial_extensions:
bot.load_extension(extension)
@bot.event
async def on_ready():
"""http://discordpy.readthedocs.io/en/rewrite/api.html#discord.on_ready"""
print(f'\n\nLogged in as: {bot.user.name} - {bot.user.id}\nVersion: {discord.__version__}\n')
# Changes our bots Playing Status. type=1(streaming) for a standard game you could remove type and url.
await bot.change_presence(game=discord.Game(name='Cogs Example', type=1, url='https://twitch.tv/kraken'))
print(f'Successfully logged in and booted...!')
bot.run('TOKEN', bot=True, reconnect=True)
import discord
from discord.ext import commands
class MembersCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
@commands.guild_only()
async def joined(self, ctx, *, member: discord.Member):
"""Says when a member joined."""
await ctx.send(f'{member.display_name} joined on {member.joined_at}')
@commands.command(name='coolbot')
async def cool_bot(self, ctx):
"""Is the bot cool?"""
await ctx.send('This bot is cool. :)')
@commands.command(name='top_role', aliases=['toprole'])
@commands.guild_only()
async def show_toprole(self, ctx, *, member: discord.Member=None):
"""Simple command which shows the members Top Role."""
if member is None:
member = ctx.author
await ctx.send(f'The top role for {member.display_name} is {member.top_role.name}')
@commands.command(name='perms', aliases=['perms_for', 'permissions'])
@commands.guild_only()
async def check_permissions(self, ctx, *, member: discord.Member=None):
"""A simple command which checks a members Guild Permissions.
If member is not provided, the author will be checked."""
if not member:
member = ctx.author
# Here we check if the value of each permission is True.
perms = '\n'.join(perm for perm, value in member.guild_permissions if value)
# And to make it look nice, we wrap it in an Embed.
embed = discord.Embed(title='Permissions for:', description=ctx.guild.name, colour=member.colour)
embed.set_author(icon_url=member.avatar_url, name=str(member))
# \uFEFF is a Zero-Width Space, which basically allows us to have an empty field name.
embed.add_field(name='\uFEFF', value=perms)
await ctx.send(content=None, embed=embed)
# Thanks to Gio for the Command.
# The setup fucntion below is neccesarry. Remember we give bot.add_cog() the name of the class in this case MembersCog.
# When we load the cog, we use the name of the file.
def setup(bot):
bot.add_cog(MembersCog(bot))
from discord.ext import commands
class OwnerCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
# Hidden means it won't show up on the default help.
@commands.command(name='load', hidden=True)
@commands.is_owner()
async def cog_load(self, ctx, *, cog: str):
"""Command which Loads a Module.
Remember to use dot path. e.g: cogs.owner"""
try:
self.bot.load_extension(cog)
except Exception as e:
await ctx.send(f'**`ERROR:`** {type(e).__name__} - {e}')
else:
await ctx.send('**`SUCCESS`**')
@commands.command(name='unload', hidden=True)
@commands.is_owner()
async def cog_unload(self, ctx, *, cog: str):
"""Command which Unloads a Module.
Remember to use dot path. e.g: cogs.owner"""
try:
self.bot.unload_extension(cog)
except Exception as e:
await ctx.send(f'**`ERROR:`** {type(e).__name__} - {e}')
else:
await ctx.send('**`SUCCESS`**')
@commands.command(name='reload', hidden=True)
@commands.is_owner()
async def cog_reload(self, ctx, *, cog: str):
"""Command which Reloads a Module.
Remember to use dot path. e.g: cogs.owner"""
try:
self.bot.unload_extension(cog)
self.bot.load_extension(cog)
except Exception as e:
await ctx.send(f'**`ERROR:`** {type(e).__name__} - {e}')
else:
await ctx.send('**`SUCCESS`**')
def setup(bot):
bot.add_cog(OwnerCog(bot))
import discord
from discord.ext import commands
"""A simple cog example with simple commands. Showcased here are some check decorators, and the use of events in cogs.
For a list of inbuilt checks:
http://dischttp://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html#checksordpy.readthedocs.io/en/rewrite/ext/commands/api.html#checks
You could also create your own custom checks. Check out:
https://github.com/Rapptz/discord.py/blob/master/discord/ext/commands/core.py#L689
For a list of events:
http://discordpy.readthedocs.io/en/rewrite/api.html#event-reference
http://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html#event-reference
"""
class SimpleCog(commands.Cog):
"""SimpleCog"""
def __init__(self, bot):
self.bot = bot
@commands.command(name='repeat', aliases=['copy', 'mimic'])
async def do_repeat(self, ctx, *, our_input: str):
"""A simple command which repeats our input.
In rewrite Context is automatically passed to our commands as the first argument after self."""
await ctx.send(our_input)
@commands.command(name='add', aliases=['plus'])
@commands.guild_only()
async def do_addition(self, ctx, first: int, second: int):
"""A simple command which does addition on two integer values."""
total = first + second
await ctx.send(f'The sum of **{first}** and **{second}** is **{total}**')
@commands.command(name='me')
@commands.is_owner()
async def only_me(self, ctx):
"""A simple command which only responds to the owner of the bot."""
await ctx.send(f'Hello {ctx.author.mention}. This command can only be used by you!!')
@commands.command(name='embeds')
@commands.guild_only()
async def example_embed(self, ctx):
"""A simple command which showcases the use of embeds.
Have a play around and visit the Visualizer."""
embed = discord.Embed(title='Example Embed',
description='Showcasing the use of Embeds...\nSee the visualizer for more info.',
colour=0x98FB98)
embed.set_author(name='MysterialPy',
url='https://gist.github.com/MysterialPy/public',
icon_url='http://i.imgur.com/ko5A30P.png')
embed.set_image(url='https://cdn.discordapp.com/attachments/84319995256905728/252292324967710721/embed.png')
embed.add_field(name='Embed Visualizer', value='[Click Here!](https://leovoel.github.io/embed-visualizer/)')
embed.add_field(name='Command Invoker', value=ctx.author.mention)
embed.set_footer(text='Made in Python with discord.py@rewrite', icon_url='http://i.imgur.com/5BFecvA.png')
await ctx.send(content='**A simple Embed for discord.py@rewrite in cogs.**', embed=embed)
@commands.Cog.listener()
async def on_member_ban(self, guild, user):
"""Event Listener which is called when a user is banned from the guild.
For this example I will keep things simple and just print some info.
Notice how because we are in a cog class we do not need to use @bot.event
For more information:
http://discordpy.readthedocs.io/en/rewrite/api.html#discord.on_member_ban
Check above for a list of events.
"""
print(f'{user.name}-{user.id} was banned from {guild.name}-{guild.id}')
# The setup fucntion below is neccesarry. Remember we give bot.add_cog() the name of the class in this case SimpleCog.
# When we load the cog, we use the name of the file.
def setup(bot):
bot.add_cog(SimpleCog(bot))
@danielpearceofficial
Copy link

Nice.

@dhkatz
Copy link

dhkatz commented Oct 29, 2017

@mixa611

Are you not running the rewrite version of discord.py?

@linkzyzvg
Copy link

linkzyzvg commented Jan 21, 2018

Thanks to ZeLarpMaster from discord for telling me that you gotta put the cogs in a folder named cogs because without his guidance idk what i was doing

@modelmat
Copy link

modelmat commented Jan 21, 2018

Mysterial, if you want to, add this somewhere: https://gist.github.com/Modelmat/ff2dc0953bf0f399fdd2083b74b4755d
A few other changes like the file names are in there too that are relevant to clearing up.

@mastersanskar
Copy link

I made a revision to my fork because "client.change_presence" parameter was changed from "game" to "activity"

@Gear-Smith
Copy link

I created another gist for the 0.16.12 version of Discord.py based on this example. -- https://gist.github.com/BananaWagon/068cef8ff640e90d3636d133fa8f72a1

@jpanahon
Copy link

I created another gist to make this example compatible with the current rewrite version (where cogs derive from commands.Cog) -- https://gist.github.com/OneEyedKnight/f0411f9a5e9dea23b96be0bf6dd86d2d

@JoshuaRose-Github
Copy link

Ok, now that's epic

@RRainmaker
Copy link

very helpful 👍

@grian32
Copy link

grian32 commented Sep 19, 2020

thank u sir

Copy link

ghost commented Sep 23, 2020

Honestly Idk why so many people use Python rather than JS to make bots. This cogs example is wonderful

@plun1331
Copy link

thank god this exists

@RRTRrRRrrtrf
Copy link

You can make cog_check instead of @commands.is_owner decorator before owner cog commands.

@devlocalhost
Copy link

do NOT change the bots status on the "on_ready" event!!

"Don't change_presence in on_ready within your Bot or Client.
Discord has a high chance to completely disconnect you during the READY or GUILD_CREATE events (1006 close code) and there is nothing you can do to prevent it.

Instead set the activity and status kwargs in the constructor of these Classes.

Basically: don't do shit in on_ready"

@TynnTynn
Copy link

TynnTynn commented Feb 4, 2021

Will use this a reference for migrating my bot to cog. thank you very much

@vsenjoyer
Copy link

vsenjoyer commented May 24, 2021

tysm super helpful ngl but I thought you're not supposed to change status and stuff in on_ready(): ?

@tooruu
Copy link

tooruu commented Aug 15, 2021

bot_example.py is bad on so many levels...

@devlocalhost
Copy link

devlocalhost commented Aug 15, 2021

bot_example.py is bad on so many levels...

Explaining why it's bad would be a good idea... Right?
@tooruu

@DejonckheereWard
Copy link

bot_example.py is bad on so many levels...

Explaining why it's bad would be a good idea... Right?
@tooruu

Yeah i don't really know either, except the change presence mentioned earlier in the comments.
Any good advice you got?

@azcoigreach
Copy link

Extremely useful. Thank you for putting this together. I wasn't finding much info on Discord command handlers and Cogs. Super informative.

@Entity303CodeMart
Copy link

Discord.py 2.0.0 How?

@bensonchow123
Copy link

is there an updated version for 2.0.0?

@stijndcl
Copy link

stijndcl commented Aug 30, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment