-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
105 lines (77 loc) · 2.59 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
from discord.ext.commands import Bot
from discord_components import (Button, ButtonStyle, DiscordComponents,
InteractionType)
import random
import json
def write_json(data, filename="faq.json"):
with open(filename,'w') as f:
json.dump(data, f, indent=4)
bot = Bot(command_prefix = "!")
colors_dict = \
{
"red":"red",
"green":"green",
"blue":"blue",
"grey":"grey",
"gray":"grey",
"r":"red",
"g":"green",
"b":"blue"
}
@bot.event
async def on_ready():
DiscordComponents(bot)
print(f"Logged in as {bot.user}!")
# ! Addfaq <color> <name> (generates id, adds it to db)
@bot.command()
async def addfaq(ctx, color_in, *content):
color = colors_dict.get(color_in)
if color is None:
return
if len(" ".join(content)) > 80:
await ctx.send(f"too long, must be 80 chars or lower {len(' '.join(content))}")
# * assign ID
rdint = random.randint(0, 99999)
with open("faq.json") as json_file:
data = json.load(json_file)
data.update({rdint:"empty"})
write_json(data)
# ! restorefaq <color> <id> <name>
@bot.command()
async def restorefaq(ctx, color_in, id, *content):
color = colors_dict.get(color_in)
if color is None:
return
if len(" ".join(content)) > 80:
await ctx.send(f"too long, must be 80 chars or lower {len(' '.join(content))}")
await ctx.send(
"", # zero width
components=[
Button(style=eval(f"ButtonStyle." + color), label=" ".join(content), id=id),
],
)
await ctx.message.delete()
@bot.event
async def on_button_click(interaction):
# await interaction.channel.send("dsa")
if interaction.responded:
return
with open("faq.json") as json_file:
data = json.load(json_file)
resp = data.get(interaction.component.id)
await interaction.respond(content = resp, type=4)
# ! Listfaq (lists with id)
@bot.command()
async def listfaq(ctx):
with open("faq.json") as json_file:
data = json.load(json_file)
await ctx.send(str(data).replace("', '", "'\n'"))
# ! changefaq <id> (new output)
@bot.command()
async def changefaq(ctx, id, *output):
with open("faq.json") as json_file:
data = json.load(json_file)
data.update({int(id):" ".join(output)})
write_json(data)
await ctx.send("done")
bot.run("NzA2NzIyMjE0MzMzOTA2OTk1.XsaN_g.hWQTdwP5FF-Fg97-6fmzrTjIa5A")