|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | from asyncio import sleep |
| 4 | +from datetime import timedelta |
4 | 5 | from logging import getLogger |
5 | 6 | from os import getenv |
6 | 7 | from typing import TYPE_CHECKING |
7 | 8 |
|
8 | | -from nextcord import Embed, Message, slash_command |
9 | | -from nextcord.ext.commands import Cog, command, is_owner |
| 9 | +from humanfriendly import parse_timespan |
| 10 | +from nextcord import Embed, Message, Permissions, TextInputStyle, slash_command |
| 11 | +from nextcord.ext.application_checks import is_owner |
| 12 | +from nextcord.ext.commands import Cog, Converter |
10 | 13 | from nextcord.ext.tasks import loop |
| 14 | +from nextcord.ui import Modal, TextInput |
| 15 | +from nextcord.utils import utcnow |
11 | 16 |
|
12 | 17 | from .extras.types import MyContext, MyInter, Notification |
13 | 18 | from .extras.views import NotificationSource, NotificationView |
14 | 19 |
|
15 | 20 | if TYPE_CHECKING: |
| 21 | + |
16 | 22 | from ..__main__ import Vibr |
17 | 23 |
|
18 | 24 |
|
19 | | -TEST = [802586580766162964, 939509053623795732] |
20 | 25 | log = getLogger(__name__) |
| 26 | +TEST_IDS = [939509053623795732] |
| 27 | + |
| 28 | + |
| 29 | +class NotificationCreate(Modal): |
| 30 | + def __init__(self): |
| 31 | + super().__init__(title="Create a Notification") |
| 32 | + |
| 33 | + self._title = TextInput( |
| 34 | + label="Enter a title for the notification.", max_length=256, required=True |
| 35 | + ) |
| 36 | + self.time = TextInput( |
| 37 | + label="Enter an expiry time for the notification", |
| 38 | + required=True, |
| 39 | + default_value="7d", |
| 40 | + ) |
| 41 | + self.description = TextInput( |
| 42 | + label="Enter a description for the notification", |
| 43 | + required=True, |
| 44 | + style=TextInputStyle.paragraph, |
| 45 | + ) |
| 46 | + |
| 47 | + self.add_item(self._title) |
| 48 | + self.add_item(self.time) |
| 49 | + self.add_item(self.description) |
| 50 | + |
| 51 | + async def callback(self, inter: MyInter): |
| 52 | + title = self._title.value |
| 53 | + notification = self.description.value |
| 54 | + |
| 55 | + try: |
| 56 | + if self.time.value is None: |
| 57 | + raise ValueError |
| 58 | + |
| 59 | + expiry = timedelta(seconds=parse_timespan(self.time.value)) |
| 60 | + except ValueError: |
| 61 | + await inter.response.send_message( |
| 62 | + "Invalid time, please try again.", ephemeral=True |
| 63 | + ) |
| 64 | + return |
| 65 | + |
| 66 | + await inter.client.db.execute( |
| 67 | + """INSERT INTO notifications(title, notification, expiry) |
| 68 | + VALUES ($1, $2, $3)""", |
| 69 | + title, |
| 70 | + notification, |
| 71 | + utcnow() + expiry, |
| 72 | + ) |
| 73 | + await inter.send("Saved in db") |
| 74 | + |
| 75 | + await inter.client.db.execute("UPDATE users SET notified=false") |
| 76 | + inter.client.notified_users.clear() |
| 77 | + await inter.send("Set all users to un-notified") |
| 78 | + log.info("Distributing notification %s", title) |
21 | 79 |
|
22 | 80 |
|
23 | 81 | class Misc(Cog): |
@@ -120,22 +178,50 @@ async def vote(self, inter: MyInter): |
120 | 178 | ) |
121 | 179 | await inter.send(embed=embed) |
122 | 180 |
|
123 | | - @command(hidden=True) |
| 181 | + @slash_command( |
| 182 | + name="create-notification", |
| 183 | + default_member_permissions=Permissions(8), |
| 184 | + guild_ids=TEST_IDS, |
| 185 | + ) |
124 | 186 | @is_owner() |
125 | | - async def notif_create(self, ctx: MyContext, title: str, *, notification: str): |
| 187 | + async def create_notification(self, inter: MyInter): |
| 188 | + """Create a notification.""" |
| 189 | + |
| 190 | + await inter.response.send_modal(NotificationCreate()) |
| 191 | + |
| 192 | + @slash_command() |
| 193 | + async def notifications(self, _: MyInter): |
| 194 | + ... |
| 195 | + |
| 196 | + @notifications.subcommand(name="toggle") |
| 197 | + async def notifications_toggle(self, inter: MyInter): |
| 198 | + """Toggle notification messages for you.""" |
| 199 | + |
| 200 | + enabled = await self.bot.db.fetchval( |
| 201 | + "SELECT notifications FROM users WHERE id=$1", inter.user.id |
| 202 | + ) |
| 203 | + |
| 204 | + if enabled is None: |
| 205 | + enabled = True |
| 206 | + |
| 207 | + new = not enabled |
| 208 | + |
126 | 209 | await self.bot.db.execute( |
127 | | - "INSERT INTO notifications(title, notification) VALUES ($1, $2)", |
128 | | - title, |
129 | | - notification, |
| 210 | + """INSERT INTO users (id, notifications) |
| 211 | + VALUES ($1, $2) |
| 212 | + ON CONFLICT (id) DO UPDATE |
| 213 | + SET notifications=$2""", |
| 214 | + inter.user.id, |
| 215 | + new, |
130 | 216 | ) |
131 | | - await ctx.send("Saved in db") |
132 | | - await self.bot.db.execute("UPDATE users SET notified=false") |
133 | | - self.bot.notified_users.clear() |
134 | | - await ctx.send("Set all users to un-notified") |
135 | | - log.info("Distributing notification %s", title) |
136 | 217 |
|
137 | | - @slash_command() |
138 | | - async def notifications(self, inter: MyInter): |
| 218 | + await inter.send( |
| 219 | + f"Notifications are now {'enabled' if new else 'disabled'} for you.", |
| 220 | + ephemeral=True, |
| 221 | + ) |
| 222 | + |
| 223 | + @notifications.subcommand(name="list") |
| 224 | + async def notifications_list(self, inter: MyInter): |
139 | 225 | """Recent announcements/notifications.""" |
140 | 226 |
|
141 | 227 | notifs = await self.bot.db.fetch("SELECT * FROM notifications ORDER BY id DESC") |
|
0 commit comments