-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·102 lines (85 loc) · 3 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/env -S poetry run python3
import interactions
import os
import d20
import random
import json
bot = interactions.Client()
if os.path.exists("config.json"):
with open("config.json") as file:
config = json.loads(file.read())
else:
config = {
"token": os.environ["TOKEN"],
}
@interactions.listen()
async def on_startup():
print("Bot is ready!")
@interactions.slash_command(name="info", description="Send information about this bot.")
async def info(ctx: interactions.SlashContext):
"""Displays the info of the bot."""
await ctx.defer()
# Create an embed to store the info
embed = interactions.Embed(
title="D&D Random Info", color=interactions.Color(0x00FFFF)
)
# Add the source code link to the embed
embed.add_field(
name="Source Code",
value="[View on GitHub](https://github.com/ryleu/discord-random)",
)
# Add the invite link to the embed
embed.add_field(
name="Invite",
value="[Add me to your own server]("
"https://discord.com/api/oauth2/authorize?client_id=803318907950596106"
"&permissions=0&scope=applications.commands%20bot)",
)
# Add the syntax link to the embed
embed.add_field(
name="Syntax",
value="[View the `/roll` syntax](https://github.com/avrae/d20#dice-syntax)",
)
# Add the bot author to the embed
embed.set_footer(text="Created by ryleu")
# Send the embed
await ctx.send(embed=embed)
@interactions.slash_command(
name="roll",
description="Rolls dice using d20's roll string format.",
options=[
interactions.SlashCommandOption(
name="roll_string", type=interactions.OptionType.STRING, required=True
)
],
)
async def roll(ctx: interactions.SlashContext, roll_string: str):
try:
roll_result = str(d20.roll(roll_string))
except d20.errors.TooManyRolls:
roll_result = "You can't roll more than 1000 total dice."
except d20.errors.RollSyntaxError as err:
escaped_params = roll_string.replace("`", "\\`")
roll_result = f"""{str(err)}```\n{escaped_params}\n{(err.col - 1) * " " + len(err.got) * "^"}\n```"""
except Exception as err:
roll_result = str(err)
await ctx.send(roll_result, ephemeral=False)
@interactions.slash_command(
name="init_channel", description="Set up a channel for rolling dice with buttons."
)
async def init_channel(ctx: interactions.SlashContext):
buttons = interactions.spread_to_rows(
*[
interactions.Button(
style=interactions.ButtonStyle.PRIMARY, label=f"d{x}", custom_id=str(x)
)
for x in (2, 4, 6, 8, 10, 12, 20, 100)
]
)
await ctx.send("Press a button to roll a die:", components=buttons)
@interactions.listen()
async def button_pressed(event: interactions.events.ButtonPressed):
ctx = event.ctx
number = int(ctx.custom_id)
await ctx.send(f"d{number} = `{random.randint(1, number)}`", ephemeral=False)
bot.start(config["token"])