forked from interactions-py/interactions.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
176 lines (136 loc) · 5.35 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import asyncio
import logging
import os
import uuid
from thefuzz import process
import interactions
from interactions import (
Client,
listen,
slash_command,
BrandColours,
slash_option,
File,
global_autocomplete,
FlatUIColours,
MaterialColours,
ButtonStyle,
)
logging.basicConfig()
logging.getLogger("interactions").setLevel(logging.DEBUG)
bot = Client()
@listen()
async def on_startup():
print(f"Logged in as {bot.user}")
@bot.event()
async def on_ready():
print("Im ready!")
@slash_command("ping")
async def ping(ctx):
action_rows = [
interactions.ActionRow(
interactions.Button(
style=interactions.ButtonStyle.DANGER,
label="Danger Button",
)
)
]
embed = interactions.Embed("Pong!", description="Pong!", color=BrandColours.BLURPLE)
for i in range(5):
embed.add_field(name=f"Field {i}", value=f"Value {uuid.uuid4()}")
await ctx.send("Pong!", components=action_rows, embeds=embed)
@slash_command("components")
async def components(ctx):
selects = [
[interactions.ChannelSelectMenu()],
[interactions.RoleSelectMenu()],
[interactions.UserSelectMenu()],
[interactions.MentionableSelectMenu()],
[interactions.StringSelectMenu("test", "test 2", "test 3")],
]
await ctx.send("Select menus", components=selects)
await ctx.send(
"Buttons",
components=[interactions.Button(label="test", style=ButtonStyle.PRIMARY)],
)
@slash_command("record", description="Record audio in your voice channel")
@slash_option("duration", "The duration of the recording", opt_type=interactions.OptionType.NUMBER, required=True)
async def record(ctx: interactions.SlashContext, duration: int) -> None:
if ctx.author.voice.channel is None:
return await ctx.send("You must be in a voice channel to use this command.")
voice_channel = ctx.author.voice.channel
voice_state = await voice_channel.connect()
recorder = await voice_state.start_recording()
await ctx.send(f"Recording for {duration} seconds")
await asyncio.sleep(duration)
await voice_state.stop_recording()
await ctx.send(
"Here is your recording", files=[File(f, file_name=f"{user_id}.mp3") for user_id, f in recorder.output.items()]
)
@slash_command("modal")
async def modal(ctx):
_modal = interactions.Modal(
interactions.ShortText(
label="Input Text",
placeholder="Placeholder",
required=True,
min_length=5,
max_length=10,
),
interactions.ParagraphText(
label="Paragraph Text",
placeholder="Placeholder",
required=True,
min_length=5,
max_length=10,
),
title="Modal",
)
await ctx.send_modal(_modal)
@listen()
async def on_component(event: interactions.events.Component):
ctx: interactions.ComponentContext = event.ctx
if ctx.values:
await ctx.send(f"Selected {ctx.values}")
else:
await ctx.send(f"Clicked {ctx.custom_id}")
@slash_command("multi_image")
async def multi_image_embed_test(ctx: interactions.SlashContext):
images = [
"https://cdn.discordapp.com/attachments/1024794413710458980/1066808750519885974/Batman_Stellar_Derp.png",
"https://cdn.discordapp.com/attachments/1024794413710458980/1066808761626402836/Batman_Derp.jpg",
]
embed = interactions.Embed(url="https://github.com/interactions-py/interactions.py", color=BrandColours.BLURPLE)
embed.set_images(*images)
await ctx.send(embeds=embed)
embed = interactions.Embed("Standard embed")
embed.set_image(images[0])
await ctx.send(embeds=embed)
def get_colour(colour: str) -> interactions.Colour:
if colour in interactions.MaterialColors.__members__:
return interactions.MaterialColors[colour]
if colour in interactions.BrandColors.__members__:
return interactions.BrandColors[colour]
if colour in interactions.FlatUIColours.__members__:
return interactions.FlatUIColours[colour]
return interactions.BrandColors.BLURPLE
@slash_command("test")
@slash_option("colour", "The colour to use", autocomplete=True, opt_type=interactions.OptionType.STRING, required=True)
@slash_option("text", "some text", autocomplete=True, opt_type=interactions.OptionType.STRING, required=True)
async def test(ctx: interactions.SlashContext, colour: str, text: str):
embed = interactions.Embed(f"{text} {colour.title()}", color=get_colour(colour))
await ctx.send(embeds=embed)
@global_autocomplete("colour")
async def colour_autocomplete(ctx: interactions.AutocompleteContext):
colours = list((BrandColours.__members__ | FlatUIColours.__members__ | MaterialColours.__members__).keys())
if not ctx.input_text:
colours = colours[:25]
else:
results = process.extract(ctx.input_text, colours, limit=25)
colour_match = sorted([result for result in results if result[1] > 50], key=lambda x: x[1], reverse=True)
colours = [colour[0] for colour in colour_match]
await ctx.send([{"name": colour.title(), "value": colour} for colour in colours])
@test.autocomplete("text")
async def text_autocomplete(ctx: interactions.AutocompleteContext):
await ctx.send([{"name": c, "value": c} for c in ["colour", "color", "shade", "hue"]])
bot.start(os.environ["TOKEN"])