main_2py.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # This example requires the 'members' privileged intent to use the Member converter
  2. # and the 'message_content' privileged intent for prefixed commands.
  3. import random
  4. import os
  5. from dotenv import load_dotenv
  6. import discord
  7. from discord.ext import commands
  8. description = """
  9. An example bot to showcase the discord.ext.commands extension module.
  10. There are a number of utility commands being showcased here.
  11. """
  12. load_dotenv()
  13. token = os.getenv("TOKEN")
  14. if token is None:
  15. raise ValueError("TOKEN not found in .env file")
  16. intents = discord.Intents.default()
  17. intents.members = True
  18. intents.message_content = True
  19. bot = commands.Bot(
  20. command_prefix=commands.when_mentioned_or("!"),
  21. description=description,
  22. intents=intents,
  23. )
  24. @bot.event
  25. async def on_ready():
  26. print(f"Logged in as {bot.user} (ID: {bot.user.id})")
  27. print("------")
  28. @bot.command()
  29. async def add(ctx: commands.Context, left: int, right: int):
  30. """Adds two numbers together."""
  31. await ctx.send(str(left + right))
  32. @bot.command()
  33. async def roll(ctx: commands.Context, dice: str):
  34. """Rolls a die in NdN format."""
  35. try:
  36. rolls, limit = map(int, dice.split("d"))
  37. except ValueError:
  38. await ctx.send("Format has to be in NdN!")
  39. return
  40. # _ is used in the generation of our result as we don't need the number that comes from the usage of range(rolls).
  41. result = ", ".join(str(random.randint(1, limit)) for _ in range(rolls))
  42. await ctx.send(result)
  43. @bot.command(description="For when you wanna settle the score some other way")
  44. async def choose(ctx: commands.Context, *choices: str):
  45. """Chooses between multiple choices."""
  46. await ctx.send(random.choice(choices))
  47. @bot.command()
  48. async def repeat(ctx: commands.Context, times: int, *, content: str = "repeating..."):
  49. """Repeats a message multiple times."""
  50. for _ in range(times):
  51. await ctx.send(content)
  52. @bot.command()
  53. async def joined(ctx: commands.Context, member: discord.Member):
  54. """Says when a member joined."""
  55. await ctx.send(f"{member.name} joined in {member.joined_at}")
  56. @bot.group()
  57. async def cool(ctx: commands.Context):
  58. """
  59. Says if a user is cool.
  60. In reality this just checks if a subcommand is being invoked.
  61. """
  62. if ctx.invoked_subcommand is None:
  63. await ctx.send(f"No, {ctx.subcommand_passed} is not cool")
  64. @cool.command(name="bot")
  65. async def _bot(ctx: commands.Context):
  66. """Is the bot cool?"""
  67. await ctx.send("Yes, the bot is cool.")
  68. bot.run(token)