Skip to content

discord

jasper-zanjani edited this page Aug 6, 2020 · 1 revision
pip install discord.py

discord.Client

client = discord.Client()

Client objects expose a decorator that is used for event handlers, functions named after various events:

  • on_ready
  • on_member_join
  • on_error
  • on_message
@client.event
async def on_ready():
  print(f'{client.user} has connected to Discord!')

Another decorator is exposed for in-chat commands (commands.Bot has to be instantiated first.)

@bot.command(name='roll_dice', help='Simulates rolling dice.')
async def roll(ctx, number_of_dice: int, number_of_sides: int):
  dice = [
    str(random.choice(range(1, number_of_sides + 1)))
    for _ in range(number_of_dice)
  ]
  await ctx.send(', '.join(dice))
client.run(token)

discord.ext.commands.Bot

bot = comands.Bot(command_prefix='!')

Clone this wiki locally