import discord from discord.ext import commands """A simple cog example with simple commands.""" class SimpleCog: """SimpleCog""" def __init__(self, bot): self.bot = bot '''Below we create our command {repeat}. Our command function must be a |coroutine| The command decorator takes in several Optional[arguments]. name : Optional[str] - Used to call the command aliases : Optional[list[str]] - Used to call the command from a list of names. The command can also be called using the defined function name |do_repeat|. Notice the first parameter of our command function after self. ctx : Context In rewrite Context is automatically passed to our commands as the first argument after self. ''' @commands.command(name='repeat', aliases=['copy', 'mimic']) async def do_repeat(self, ctx, *, inp: str): """A simple command which repeats our input. inp : [str]""" '''Here we see that |send| automatically retrieves the channel contained in context for us. This is similar to bot.say - (From async). It will send the message to the channel which the command was invoked.''' await ctx.send(f'{inp}') '''Below we create our command {add}. Our command function must be a |coroutine|. For this command we have added a check |guild_only|. This makes sure that our command can only be used in a guild. If it is used outside of a guild, the check will fail and an error will be sent to |on_command_error|. 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 ''' @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. first : [int] second : [int]""" total = first + second await ctx.send(f'The sum of **{first}** and **{second}** is **{total}**') '''Below we create our command {me}. Our command function must be a |coroutine|. For this command we have added a check |is_owner|. This makes sure that our command can only be used by the bot owner. ''' @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. Showcased here is [Member.mention] which allows us to mention a member with ease. In this case the person who invoked the command, otherwise known as [author]. http://discordpy.readthedocs.io/en/rewrite/api.html#discord.Member.mention""" await ctx.send(f'Hello {ctx.author.mention}. I only respond to you!') def setup(bot): bot.add_cog(SimpleCog(bot))