-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotes.py
168 lines (147 loc) · 4.55 KB
/
notes.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
import discord
from core import Cog, Context, models
async def note_write(ctx : Context, note_title, note_content):
# Create a new note
note = {
note_title: note_content
}
user = await models.UserModel.get_or_none(user_id = ctx.author.id)
if user:
user.notes = {**user.notes, **note}
await user.save()
else:
await models.UserModel.update_or_create(
user_id=ctx.author.id,
user_name=ctx.author.name,
user_discriminator=ctx.author.discriminator,
notes=note,
baned=False,
commands_used=1
)
async def get_notes(ctx : Context):
# Return a list of note titles for selection
user = await models.UserModel.get_or_none(user_id = ctx.author.id)
if user:
# return a dictionary of note titles and content
notes = user.notes.items()
return notes
else:
return None
async def note_delete(ctx : Context, note_title):
# Delete a note
user = await models.UserModel.get_or_none(user_id = ctx.author.id)
if user:
notes = user.notes
if note_title in notes:
del notes[note_title]
await user.save()
return True
else:
return False
else:
return False
class NoteSelect(discord.ui.Select):
def __init__(self, notes) -> None:
super().__init__(
placeholder="Choose a note",
options=[
discord.SelectOption(
label=note[0], # Title
description=note[1][:100]
)
for note in notes
],
)
async def callback(self, interaction: discord.Interaction):
options = self.options
note_title = interaction.data["values"][0]
assert isinstance(note_title, str)
for option in options:
if option.label == note_title:
note_content = option.description
break
# find which element in the list has value=note_title
embed = discord.Embed(
title=note_title,
description=note_content,
color=0x5865F2,
)
await interaction.response.send_message(
embed=embed,
ephemeral=True,
)
class Notes(Cog):
"""
Notes commands
"""
@discord.slash_command(
integration_types={
discord.IntegrationType.guild_install,
discord.IntegrationType.user_install,
},
name="note",
description="Write a note",
)
@discord.option(
"note_title",
description="The title of the note",
type=str
)
@discord.option(
"note_content",
description="The content of the note",
type=str
)
async def note_write_command(self, ctx: Context, note_title: str, note_content: str):
"""Write a note"""
await ctx.defer()
await note_write(ctx, note_title, note_content)
await ctx.respond(content="Note written")
@discord.slash_command(
integration_types={
discord.IntegrationType.guild_install,
discord.IntegrationType.user_install,
},
name="note-view",
description="View a note",
)
async def note_view_command(self, ctx: Context):
"""View a note"""
await ctx.defer()
assert self.bot.user
embed = discord.Embed(
title="Notes",
description=(
"Choose a note from the dropdown below to see the content."
),
colour=0x5865F2,
)
embed.set_thumbnail(url=ctx.author.avatar.url)
notes = await get_notes(ctx)
if notes:
await ctx.respond(embed=embed, view=discord.ui.View(NoteSelect(notes))
)
else:
await ctx.respond(content="No notes found")
@discord.slash_command(
integration_types={
discord.IntegrationType.guild_install,
discord.IntegrationType.user_install,
},
name="note-delete",
description="Delete a note",
)
@discord.option(
"note_title",
description="The title of the note",
type=str
)
async def note_delete_command(self, ctx: Context, note_title: str):
"""Delete a note"""
await ctx.defer()
if await note_delete(ctx, note_title):
await ctx.respond(content="Note deleted")
else:
await ctx.respond(content="Note not found")
def setup(bot):
bot.add_cog(Notes(bot))