Преглед изворни кода

big refactor. Divided project into multiple .py files each containing single class for one type of functional.

tenessy0570 пре 3 година
родитељ
комит
31d39e5e94
5 измењених фајлова са 144 додато и 125 уклоњено
  1. 4 125
      __init__.py
  2. 9 0
      common_client_class_to_run.py
  3. 74 0
      on_messages.py
  4. 31 0
      on_ready.py
  5. 26 0
      reactions.py

+ 4 - 125
__init__.py

@@ -1,130 +1,9 @@
 import environment_vars
-import discord
 from os import environ
-from img_urls import good_face_url
-from decorators import (
-    author_is_not_bot,
-    notify_if_wrong_command
-)
-from utils import (
-    on_ready_print,
-    receive_message_then_send,
-    get_commands_from_file,
-    message_is_song_name,
-    get_video_url_by_song_name,
-    get_embed,
-    get_commands_list_to_send,
-    in_bot_channel,
-    create_message_and_add_reactions,
-    create_and_get_roles_dict,
-    get_roles_for_send,
-    get_role_from_payload,
-    reacted_user_is_bot,
-    get_on_delete_content, get_reacted_user
-)
+from discord import Intents
+from common_client_class_to_run import Client
 
-
-class MyClient(discord.Client):
-    _help_commands = get_commands_from_file("commands_list_divided_with_newline")
-    _help_commands_for_output = ('~ ' + command for command in _help_commands)
-    _help_commands_for_output = '\n'.join(_help_commands_for_output)
-
-    _emojis = {
-        ":smiling_face_with_3_hearts:": u"\U0001F970",
-        ":train:": u"\U0001F68B",
-        ":kimono:": u"\U0001F458"
-    }
-
-    _roles = create_and_get_roles_dict(_emojis)
-
-    async def on_ready(self):
-        await on_ready_print(self)
-        # _channel = None
-        #
-        # for channel in self.get_all_channels():
-        #     if channel.name != 'bot':
-        #         _channel = channel
-        #
-        # _roles_for_send = await get_roles_for_send(self)
-        # await create_message_and_add_reactions(self, _channel, _roles_for_send)
-
-    @author_is_not_bot
-    @notify_if_wrong_command
-    async def on_message(self, message):
-        if not await in_bot_channel(message=message):
-            return
-
-        if await receive_message_then_send(message, "ping", "pong"):
-            return
-
-        if await receive_message_then_send(message, "avatar"):
-            image_to_send = await get_embed(self.user.avatar_url)
-            await message.channel.send(embed=image_to_send)
-            return
-
-        if await receive_message_then_send(message, "!help"):
-            await message.channel.send(await get_commands_list_to_send(self))
-            return
-
-        if message.attachments:
-            await message.delete()
-            await message.channel.send('No attachments! :)')
-            return
-
-        if await receive_message_then_send(message, "face"):
-            image_to_send = await get_embed(url=good_face_url)
-            await message.channel.send(embed=image_to_send)
-            return
-
-        if await message_is_song_name(message):
-
-            try:
-                url = await get_video_url_by_song_name(message)
-            except (NameError, discord.errors.HTTPException):
-                await message.channel.send('Bad song name!')
-                return
-
-            await message.channel.send(url)
-            return
-
-        return True
-
-    async def on_typing(self, channel, user, when):
-        if not await in_bot_channel(channel=channel):
-            return
-
-        await channel.send(f"{user.mention} started typing something on {when}. I saw it!")
-
-    async def on_message_delete(self, message):
-        if not await in_bot_channel(message=message):
-            return
-
-        message_content = await get_on_delete_content(message)
-
-        await message.channel.send(
-            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 await reacted_user_is_bot(self, payload) or await in_bot_channel(channel=channel):
-            return
-
-        role = await get_role_from_payload(self, payload, channel)
-        await payload.member.add_roles(role)
-
-    async def on_raw_reaction_remove(self, payload):
-        channel = self.get_channel(payload.channel_id)
-        if await reacted_user_is_bot(self, payload) or await in_bot_channel(channel=channel):
-            return
-
-        role = await get_role_from_payload(self, payload, channel)
-        reacted_user = await get_reacted_user(self, payload)
-
-        await reacted_user.remove_roles(role)
-
-
-intents = discord.Intents().all()
-client = MyClient(intents=intents)
 bot_token = environ.get('bot_token')
+
+client = Client(intents=Intents.all())
 client.run(bot_token)

+ 9 - 0
common_client_class_to_run.py

@@ -0,0 +1,9 @@
+from reactions import Reactions
+from discord import Client as ClientToInherit
+from on_messages import Messages
+from on_ready import Ready
+from reactions import Reactions
+
+
+class Client(ClientToInherit, Ready, Messages, Reactions):
+    pass

+ 74 - 0
on_messages.py

@@ -0,0 +1,74 @@
+from discord.errors import HTTPException
+from utils import (
+    in_bot_channel,
+    receive_message_then_send,
+    get_commands_list_to_send,
+    get_embed,
+    message_is_song_name,
+    get_video_url_by_song_name,
+    get_on_delete_content
+)
+from img_urls import good_face_url
+from decorators import (
+    author_is_not_bot,
+    notify_if_wrong_command
+)
+
+
+class Messages:
+    @author_is_not_bot
+    @notify_if_wrong_command
+    async def on_message(self, message):
+        if not await in_bot_channel(message=message):
+            return
+
+        if await receive_message_then_send(message, "ping", "pong"):
+            return
+
+        if await receive_message_then_send(message, "avatar"):
+            image_to_send = await get_embed(self.user.avatar_url)
+            await message.channel.send(embed=image_to_send)
+            return
+
+        if await receive_message_then_send(message, "!help"):
+            await message.channel.send(await get_commands_list_to_send(self))
+            return
+
+        if message.attachments:
+            await message.delete()
+            await message.channel.send('No attachments! :)')
+            return
+
+        if await receive_message_then_send(message, "face"):
+            image_to_send = await get_embed(url=good_face_url)
+            await message.channel.send(embed=image_to_send)
+            return
+
+        if await message_is_song_name(message):
+
+            try:
+                url = await get_video_url_by_song_name(message)
+            except (NameError, HTTPException):
+                await message.channel.send('Bad song name!')
+                return
+
+            await message.channel.send(url)
+            return
+
+        return True
+
+    async def on_typing(self, channel, user, when):
+        if not await in_bot_channel(channel=channel):
+            return
+
+        await channel.send(f"{user.mention} started typing something on {when}. I saw it!")
+
+    async def on_message_delete(self, message):
+        if not await in_bot_channel(message=message):
+            return
+
+        message_content = await get_on_delete_content(message)
+
+        await message.channel.send(
+            f"{message.author.mention}'s message has just been deleted which was {message_content}"
+        )

+ 31 - 0
on_ready.py

@@ -0,0 +1,31 @@
+from utils import (
+    get_commands_from_file,
+    create_and_get_roles_dict,
+    on_ready_print,
+    create_message_and_add_reactions
+)
+
+
+class Ready:
+    _help_commands = get_commands_from_file("commands_list_divided_with_newline")
+    _help_commands_for_output = ('~ ' + command for command in _help_commands)
+    _help_commands_for_output = '\n'.join(_help_commands_for_output)
+
+    _emojis = {
+        ":smiling_face_with_3_hearts:": u"\U0001F970",
+        ":train:": u"\U0001F68B",
+        ":kimono:": u"\U0001F458"
+    }
+
+    _roles = create_and_get_roles_dict(_emojis)
+
+    async def on_ready(self):
+        await on_ready_print(self)
+        # _channel = None
+        #
+        # for channel in self.get_all_channels():
+        #     if channel.name != 'bot':
+        #         _channel = channel
+        #
+        # _roles_for_send = await get_roles_for_send(self)
+        # await create_message_and_add_reactions(self, _channel, _roles_for_send)

+ 26 - 0
reactions.py

@@ -0,0 +1,26 @@
+from utils import (
+    reacted_user_is_bot,
+    in_bot_channel,
+    get_role_from_payload,
+    get_reacted_user
+)
+
+
+class Reactions:
+    async def on_raw_reaction_add(self, payload):
+        channel = self.get_channel(payload.channel_id)
+        if await reacted_user_is_bot(self, payload) or await in_bot_channel(channel=channel):
+            return
+
+        role = await get_role_from_payload(self, payload, channel)
+        await payload.member.add_roles(role)
+
+    async def on_raw_reaction_remove(self, payload):
+        channel = self.get_channel(payload.channel_id)
+        if await reacted_user_is_bot(self, payload) or await in_bot_channel(channel=channel):
+            return
+
+        role = await get_role_from_payload(self, payload, channel)
+        reacted_user = await get_reacted_user(self, payload)
+
+        await reacted_user.remove_roles(role)