- 
      
 - 
        
Save barbosso/efb21e4029aefc2043e38b90b88918c7 to your computer and use it in GitHub Desktop.  
    A Cogs Example for the rewrite version of - discord.py
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | 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, msg): | |
| """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 msg.guild.id is None: | |
| # 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, msg) | |
| initial_extensions = ('cogs.simple', | |
| 'cogs.handler',) | |
| bot = commands.Bot(command_prefix=get_prefix, description='A Rewrite Cog Example') | |
| @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')) | |
| # Here we load our extensions listed above in [initial_extensions]. | |
| if __name__ == '__main__': | |
| for extension in initial_extensions: | |
| try: | |
| bot.load_extension(extension) | |
| except Exception as e: | |
| print(f'Failed to load extension {extension}.', file=sys.stderr) | |
| traceback.print_exc() | |
| print(f'Successfully logged in and booted...!') | |
| bot.run('TOKEN', bot=True, reconnect=True) | 
  
    
      This file contains hidden or 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
    
  
  
    
  | 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: | |
| """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: | |
| https://leovoel.github.io/embed-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) | |
| def setup(bot): | |
| bot.add_cog(SimpleCog(bot)) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment