Browse Source

Basic bot example now shows documenting for the built-in help command.

pull/95/head
Rapptz 9 years ago
parent
commit
8d76e7095e
  1. 24
      examples/basic_bot.py

24
examples/basic_bot.py

@ -2,7 +2,11 @@ import discord
from discord.ext import commands from discord.ext import commands
import random import random
bot = commands.Bot(command_prefix='?') description = '''An example bot to showcase the discord.ext.commands extension
module.
There are a number of utility commands being showcased here.'''
bot = commands.Bot(command_prefix='?', description=description)
@bot.event @bot.event
async def on_ready(): async def on_ready():
@ -13,10 +17,12 @@ async def on_ready():
@bot.command() @bot.command()
async def add(left : int, right : int): async def add(left : int, right : int):
"""Adds two numbers together."""
await bot.say(left + right) await bot.say(left + right)
@bot.command() @bot.command()
async def roll(dice : str): async def roll(dice : str):
"""Rolls a dice in NdN format."""
try: try:
rolls, limit = map(int, dice.split('d')) rolls, limit = map(int, dice.split('d'))
except Exception: except Exception:
@ -26,26 +32,34 @@ async def roll(dice : str):
result = ', '.join(str(random.randint(1, limit)) for r in range(rolls)) result = ', '.join(str(random.randint(1, limit)) for r in range(rolls))
await bot.say(result) await bot.say(result)
@bot.command() @bot.command(description='For when you wanna settle the score some other way')
async def choose(*choices : str): async def choose(*choices : str):
"""Chooses between multiple choices."""
await bot.say(random.choice(choices)) await bot.say(random.choice(choices))
@bot.command() @bot.command()
async def repeat(times : int, content='repeating...'): async def repeat(times : int, content='repeating...'):
"""Repeats a message multiple times."""
for i in range(times): for i in range(times):
await bot.say(content) await bot.say(content)
@bot.command() @bot.command()
async def joined(member : discord.Member): async def joined(member : discord.Member):
"""Says when a member joined."""
await bot.say('{0.name} joined in {0.joined_at}'.format(member)) await bot.say('{0.name} joined in {0.joined_at}'.format(member))
@bot.group(pass_context=True) @bot.group(pass_context=True)
async def cool(ctx): async def cool(ctx):
"""Says if a user is cool.
In reality this just checks if a subcommand is being invoked.
"""
if ctx.invoked_subcommand is None: if ctx.invoked_subcommand is None:
await bot.say('No, {0.subcommand_passed} is not cool'.format(ctx)) await bot.say('No, {0.subcommand_passed} is not cool'.format(ctx))
@cool.command() @cool.command(name='bot')
async def bob(): async def _bot():
await bot.say('Yes, bob is cool.') """Is the bot cool?"""
await bot.say('Yes, the bot is cool.')
bot.run('email', 'password') bot.run('email', 'password')

Loading…
Cancel
Save