Fixed DM registration errors caused by v2.0.1
Clarified that the bot can also send alert DMs
This commit is contained in:
GaMeNu 2023-10-11 14:32:05 +03:00
parent 58bba3e212
commit ea834b6e6f
3 changed files with 24 additions and 10 deletions

View File

@ -8,7 +8,9 @@
This is a bot that connects to the HFC's servers and sends real-time notifications about alerts in Israel. This is a bot that connects to the HFC's servers and sends real-time notifications about alerts in Israel.
### Setup ### Setup
Just invite the bot to a server (see "Links" below), and /register a channel to start receiving notifications! Invite the bot to a server, and /register a channel, and you're ready to go!
Alternatively, you can DM the bot to receive alerts directly to your DMs!
Please do note that the bot instance listed here is hosted on a private machine, and may be a bit slow (although it doesn't seem to be an issue yet). Feel free to host your own version! Please do note that the bot instance listed here is hosted on a private machine, and may be a bit slow (although it doesn't seem to be an issue yet). Feel free to host your own version!

View File

@ -10,6 +10,7 @@ from discord import app_commands
import logging import logging
import db_access
from db_access import DBAccess from db_access import DBAccess
from markdown import md from markdown import md
@ -190,6 +191,9 @@ class Notificator(commands.Cog):
server_id = None server_id = None
channel_id = intr.user.id channel_id = intr.user.id
await self.attempt_registration(intr, channel_id, server_id)
async def attempt_registration(self, intr, channel_id, server_id):
if self.db.get_channel(channel_id) is not None: if self.db.get_channel(channel_id) is not None:
try: try:
await intr.response.send_message(f'Channel #{intr.channel.name} is already receiving HFC alerts.') await intr.response.send_message(f'Channel #{intr.channel.name} is already receiving HFC alerts.')
@ -199,7 +203,6 @@ class Notificator(commands.Cog):
if server_id is not None and self.db.get_server(server_id) is None: if server_id is not None and self.db.get_server(server_id) is None:
self.db.add_server(server_id, 'he') self.db.add_server(server_id, 'he')
self.db.add_channel(channel_id, server_id, 'he') self.db.add_channel(channel_id, server_id, 'he')
try: try:
await intr.response.send_message(f'Channel #{intr.channel.name} will now receive HFC alerts.') await intr.response.send_message(f'Channel #{intr.channel.name} will now receive HFC alerts.')
@ -209,7 +212,11 @@ class Notificator(commands.Cog):
@register_channel.error @register_channel.error
async def register_channel_error(self, intr: discord.Interaction, error): async def register_channel_error(self, intr: discord.Interaction, error):
if isinstance(error, app_commands.MissingPermissions): if isinstance(error, app_commands.MissingPermissions):
await intr.response.send_message('Error: You are missing the Manage Channels permission.') if intr.guild is not None:
await intr.response.send_message('Error: You are missing the Manage Channels permission.')
return
await self.attempt_registration(intr, intr.user.id, None)
@app_commands.command(name='unregister', description='Stop a channel from receiving HFC alerts (Requires Manage Channels)') @app_commands.command(name='unregister', description='Stop a channel from receiving HFC alerts (Requires Manage Channels)')
@app_commands.checks.has_permissions(manage_channels=True) @app_commands.checks.has_permissions(manage_channels=True)
@ -220,8 +227,10 @@ class Notificator(commands.Cog):
if channel is None: if channel is None:
channel = self.db.get_channel(intr.user.id) channel = self.db.get_channel(intr.user.id)
if channel is None: await self.attempt_unregistration(intr, channel)
async def attempt_unregistration(self, intr, channel):
if channel is None:
try: try:
await intr.response.send_message(f'Channel #{intr.channel.name} is not yet receiving HFC alerts') await intr.response.send_message(f'Channel #{intr.channel.name} is not yet receiving HFC alerts')
except AttributeError: except AttributeError:
@ -229,19 +238,21 @@ class Notificator(commands.Cog):
return return
if channel.server_id is not None: if channel.server_id is not None:
self.db.remove_channel(channel_id) self.db.remove_channel(channel.id)
else: else:
self.db.remove_channel(intr.user.id) self.db.remove_channel(intr.user.id)
try: try:
await intr.response.send_message(f'Channel #{intr.channel.name} will no longer receive HFC alerts') await intr.response.send_message(f'Channel #{intr.channel.name} will no longer receive HFC alerts')
except AttributeError: except AttributeError:
await intr.response.send_message(f'This channel will no longer receive HFC alerts') await intr.response.send_message(f'This channel will no longer receive HFC alerts')
@unregister_channel.error @unregister_channel.error
async def register_channel_error(self, intr: discord.Interaction, error): async def unregister_channel_error(self, intr: discord.Interaction, error):
if isinstance(error, app_commands.MissingPermissions): if isinstance(error, app_commands.MissingPermissions):
await intr.response.send_message('Error: You are missing the Manage Channels permission.') if intr.guild is not None:
await intr.response.send_message('Error: You are missing the Manage Channels permission.')
return
await self.attempt_unregistration(intr, self.db.get_channel(intr.user.id))
@app_commands.command(name='about', description='Info about the bot') @app_commands.command(name='about', description='Info about the bot')
async def about_bot(self, intr: discord.Interaction): async def about_bot(self, intr: discord.Interaction):
@ -255,7 +266,8 @@ class Notificator(commands.Cog):
value='This is a bot that connects to the HFC\'s servers and sends real-time notifications about alerts in Israel.', value='This is a bot that connects to the HFC\'s servers and sends real-time notifications about alerts in Israel.',
inline=False) inline=False)
e.add_field(name='Setup', e.add_field(name='Setup',
value='Just invite the bot to a server (see \"Links\" below), and /register a channel to start receiving notifications.\n' value='Just invite the bot to a server (see Links below), and /register a channel to start receiving notifications.\n'
'Alternatively, you can /register a DM directly with the bot.\n'
'Please do note that the main version of the bot is hosted on a private machine, so it may be a bit slow (although it doesn\'t seem to be an issue yet).\n' 'Please do note that the main version of the bot is hosted on a private machine, so it may be a bit slow (although it doesn\'t seem to be an issue yet).\n'
'Feel free to host your own version!', 'Feel free to host your own version!',
inline=False) inline=False)

View File

@ -23,7 +23,7 @@ class District:
class Channel: class Channel:
def __init__(self, id: int, server_id: int, channel_lang: str): def __init__(self, id: int, server_id: int | None, channel_lang: str):
self.id = id self.id = id
self.server_id = server_id self.server_id = server_id
self.channel_lang = channel_lang self.channel_lang = channel_lang