Browse Source

tried to handle event of adding reaction to any message

tenessy0570 3 years ago
parent
commit
21295fcaff
2 changed files with 59 additions and 24 deletions
  1. 30 12
      __init__.py
  2. 29 12
      utils.py

+ 30 - 12
__init__.py

@@ -15,7 +15,9 @@ from utils import (
     get_embed,
     get_embed,
     get_commands_list_to_send,
     get_commands_list_to_send,
     is_embed,
     is_embed,
-    in_bot_channel
+    in_bot_channel,
+    get_message_by_id,
+    get_reaction_info
 )
 )
 
 
 
 
@@ -25,24 +27,28 @@ class MyClient(discord.Client):
     _help_commands_for_output = '\n'.join(_help_commands_for_output)
     _help_commands_for_output = '\n'.join(_help_commands_for_output)
 
 
     async def on_ready(self):
     async def on_ready(self):
-        on_ready_print(self)
+        await on_ready_print(self)
+        for channel in self.get_all_channels():
+            if channel.name == 'bot':
+                _channel = channel
+        await _channel.send("Loaded")
 
 
     @author_is_not_bot
     @author_is_not_bot
     @notify_if_wrong_command
     @notify_if_wrong_command
     async def on_message(self, message):
     async def on_message(self, message):
-        if not in_bot_channel(message=message):
+        if not await in_bot_channel(message=message):
             return
             return
 
 
         if await receive_message_then_send(message, "ping", "pong"):
         if await receive_message_then_send(message, "ping", "pong"):
             return
             return
 
 
         if await receive_message_then_send(message, "avatar"):
         if await receive_message_then_send(message, "avatar"):
-            image_to_send = get_embed(self.user.avatar_url)
+            image_to_send = await get_embed(self.user.avatar_url)
             await message.channel.send(embed=image_to_send)
             await message.channel.send(embed=image_to_send)
             return
             return
 
 
         if await receive_message_then_send(message, "!help"):
         if await receive_message_then_send(message, "!help"):
-            await message.channel.send(get_commands_list_to_send(self))
+            await message.channel.send(await get_commands_list_to_send(self))
             return
             return
 
 
         if message.attachments:
         if message.attachments:
@@ -51,16 +57,16 @@ class MyClient(discord.Client):
             return
             return
 
 
         if await receive_message_then_send(message, "face"):
         if await receive_message_then_send(message, "face"):
-            image_to_send = get_embed(url=good_face_url)
+            image_to_send = await get_embed(url=good_face_url)
             await message.channel.send(embed=image_to_send)
             await message.channel.send(embed=image_to_send)
             return
             return
 
 
-        if message_is_song_name(message):
+        if await message_is_song_name(message):
 
 
             try:
             try:
-                url = get_video_url_by_song_name(message)
-            except NameError as error_info:
-                await message.channel.send(error_info)
+                url = await get_video_url_by_song_name(message)
+            except (NameError, discord.errors.HTTPException):
+                await message.channel.send('Bad song name!')
                 return
                 return
 
 
             await message.channel.send(url)
             await message.channel.send(url)
@@ -69,13 +75,13 @@ class MyClient(discord.Client):
         return True
         return True
 
 
     async def on_typing(self, channel, user, when):
     async def on_typing(self, channel, user, when):
-        if not in_bot_channel(channel=channel):
+        if not await in_bot_channel(channel=channel):
             return
             return
 
 
         await channel.send(f"{user.mention} started typing something on {when}. I saw it!")
         await channel.send(f"{user.mention} started typing something on {when}. I saw it!")
 
 
     async def on_message_delete(self, message):
     async def on_message_delete(self, message):
-        if not in_bot_channel(message=message):
+        if not await in_bot_channel(message=message):
             return
             return
 
 
         message_content = f'"{message.content}"' \
         message_content = f'"{message.content}"' \
@@ -85,6 +91,18 @@ class MyClient(discord.Client):
             f"{message.author.mention}'s message has just been deleted which was {message_content}"
             f"{message.author.mention}'s message has just been deleted which was {message_content}"
         )
         )
 
 
+    async def on_raw_reaction_add(self, payload):
+        channel = self.get_channel(payload.channel_id)
+        if not await in_bot_channel(channel=channel):
+            return
+
+        emoji, who_reacted, message_id = await get_reaction_info(payload)
+        message = await get_message_by_id(channel, message_id)
+
+        await channel.send(
+            f"{who_reacted.name} has just reacted to {message.author.name}'s message with {emoji.name}!"
+        )
+
 
 
 bot_token = environ.get('bot_token')
 bot_token = environ.get('bot_token')
 client = MyClient()
 client = MyClient()

+ 29 - 12
utils.py

@@ -2,7 +2,7 @@ import discord
 from youtubesearchpython import VideosSearch
 from youtubesearchpython import VideosSearch
 
 
 
 
-def on_ready_print(self):
+async def on_ready_print(self):
     print("---------")
     print("---------")
     print("Logged as")
     print("Logged as")
     print(self.user.name)
     print(self.user.name)
@@ -23,11 +23,11 @@ def get_commands_from_file(filename: str) -> tuple:
     return _help_commands
     return _help_commands
 
 
 
 
-def message_is_song_name(message):
+async def message_is_song_name(message):
     return message.content.split(' ')[0] == '!song'
     return message.content.split(' ')[0] == '!song'
 
 
 
 
-def _get_song_name_from_message(message):
+async def _get_song_name_from_message(message):
     parsed_msg_list = message.content.split(' ')
     parsed_msg_list = message.content.split(' ')
     song_name = ' '.join(parsed_msg_list[1:])
     song_name = ' '.join(parsed_msg_list[1:])
 
 
@@ -37,31 +37,48 @@ def _get_song_name_from_message(message):
     return song_name
     return song_name
 
 
 
 
-def _get_movie_id(videos_search):
+async def _get_movie_id(videos_search):
     return videos_search.result()['result'][0]['id']
     return videos_search.result()['result'][0]['id']
 
 
 
 
-def get_video_url_by_song_name(message):
+async def get_video_url_by_song_name(message):
     url = 'https://www.youtube.com/watch?v='
     url = 'https://www.youtube.com/watch?v='
-    song_name = _get_song_name_from_message(message)
-    videos_search = VideosSearch(song_name, limit=1)
-    video_id = _get_movie_id(videos_search)
+    song_name = await _get_song_name_from_message(message)
+
+    if song_name:
+        videos_search = VideosSearch(song_name, limit=1)
+    else:
+        raise NameError
+
+    try:
+        video_id = await _get_movie_id(videos_search)
+    except IndexError:
+        raise NameError
+
     return url + video_id
     return url + video_id
 
 
 
 
-def get_embed(url):
+async def get_embed(url):
     return discord.Embed().set_image(url=url)
     return discord.Embed().set_image(url=url)
 
 
 
 
-def get_commands_list_to_send(self):
+async def get_commands_list_to_send(self):
     return "Available commands: \n" + self._help_commands_for_output
     return "Available commands: \n" + self._help_commands_for_output
 
 
 
 
-def is_embed(message):
+async def is_embed(message):
     return message.content == ""
     return message.content == ""
 
 
 
 
-def in_bot_channel(channel=None, message=None):
+async def in_bot_channel(channel=None, message=None):
     if message:
     if message:
         channel = message.channel
         channel = message.channel
     return channel.name == 'bot'
     return channel.name == 'bot'
+
+
+async def get_message_by_id(channel, message_id):
+    return await discord.GroupChannel.fetch_message(self=channel, id=message_id)
+
+
+async def get_reaction_info(payload):
+    return payload.emoji, payload.member, payload.message_id