| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- # This example requires the 'members' privileged intent to use the Member converter
- # and the 'message_content' privileged intent for prefixed commands.
- import random
- import os
- from dotenv import load_dotenv
- import discord
- from discord.ext import commands
- description = """
- An example bot to showcase the discord.ext.commands extension module.
- There are a number of utility commands being showcased here.
- """
- load_dotenv()
- token = os.getenv("TOKEN")
- if token is None:
- raise ValueError("TOKEN not found in .env file")
- intents = discord.Intents.default()
- intents.members = True
- intents.message_content = True
- bot = commands.Bot(
- command_prefix=commands.when_mentioned_or("!"),
- description=description,
- intents=intents,
- )
- @bot.event
- async def on_ready():
- print(f"Logged in as {bot.user} (ID: {bot.user.id})")
- print("------")
- @bot.command()
- async def add(ctx: commands.Context, left: int, right: int):
- """Adds two numbers together."""
- await ctx.send(str(left + right))
- @bot.command()
- async def roll(ctx: commands.Context, dice: str):
- """Rolls a die in NdN format."""
- try:
- rolls, limit = map(int, dice.split("d"))
- except ValueError:
- await ctx.send("Format has to be in NdN!")
- return
- # _ is used in the generation of our result as we don't need the number that comes from the usage of range(rolls).
- result = ", ".join(str(random.randint(1, limit)) for _ in range(rolls))
- await ctx.send(result)
- @bot.command(description="For when you wanna settle the score some other way")
- async def choose(ctx: commands.Context, *choices: str):
- """Chooses between multiple choices."""
- await ctx.send(random.choice(choices))
- @bot.command()
- async def repeat(ctx: commands.Context, times: int, *, content: str = "repeating..."):
- """Repeats a message multiple times."""
- for _ in range(times):
- await ctx.send(content)
- @bot.command()
- async def joined(ctx: commands.Context, member: discord.Member):
- """Says when a member joined."""
- await ctx.send(f"{member.name} joined in {member.joined_at}")
- @bot.group()
- async def cool(ctx: commands.Context):
- """
- Says if a user is cool.
- In reality this just checks if a subcommand is being invoked.
- """
- if ctx.invoked_subcommand is None:
- await ctx.send(f"No, {ctx.subcommand_passed} is not cool")
- @cool.command(name="bot")
- async def _bot(ctx: commands.Context):
- """Is the bot cool?"""
- await ctx.send("Yes, the bot is cool.")
- bot.run(token)
|