From 9f7ffff43c6a4e9a32b73b1b04e65a2aff837c1c Mon Sep 17 00:00:00 2001 From: klemek Date: Wed, 6 Jan 2021 12:09:10 +0100 Subject: [PATCH] transition to miniscord --- .gitignore | 3 +- bot.py | 108 +++++++++-------------------------------------- help.py | 31 -------------- requirements.txt | 1 + 4 files changed, 22 insertions(+), 121 deletions(-) mode change 100644 => 100755 .gitignore mode change 100644 => 100755 bot.py delete mode 100644 help.py mode change 100644 => 100755 requirements.txt diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 index 1171896..738e240 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ venv __pycache__ .env -error_* \ No newline at end of file +error_* +*.log diff --git a/bot.py b/bot.py old mode 100644 new mode 100755 index 2d72cac..e3d8ec4 --- a/bot.py +++ b/bot.py @@ -1,92 +1,22 @@ -import os -import discord -from datetime import datetime -from dotenv import load_dotenv +from miniscord import Bot -# Custom libs import emotes -import help -from utils import debug -VERSION = "1.3" -t0 = datetime.now() - -# Loading token -load_dotenv() -token = os.getenv('DISCORD_TOKEN') - -client = discord.Client() - - -async def info(message, args): - """ - Computes the %info command - - :param message: message sent - :type message: :class:`discord.Message` - :param args: arguments of the command - :type args: list[:class:`str`] - """ - await message.channel.send(f"```Discord Analyst v{VERSION} started at {t0:%Y-%m-%d %H:%M}```") - - -COMMANDS = { - "%help": help.compute, - "%emotes": emotes.compute, - "%info": info -} - - -@client.event -async def on_ready(): - """ - Called when client is connected - """ - # Change status - await client.change_presence( - activity=discord.Game(f"v{VERSION} | %help"), - status=discord.Status.online - ) - # Debug connected guilds - print(f'{client.user} v{VERSION} has connected to Discord\nto the following guilds:') - for guild in client.guilds: - print(f'- {guild.name}(id: {guild.id})') - - -@client.event -async def on_message(message): - """ - Called when a message is sent to any channel on any guild - - :param message: message sent - :type message: discord.Message - """ - - # Ignore self messages - if message.author == client.user: - return - - args = message.content.split(" ") - - if len(args) < 1 or args[0] not in COMMANDS: - return - - debug(message, f"command '{message.content}'") - - # Check if bot can respond on current channel or DM user - permissions = message.channel.permissions_for(message.guild.me) - if not permissions.send_messages: - debug(message, f"missing 'send_messages' permission") - await message.author.create_dm() - await message.author.dm_channel.send( - f"Hi, this bot doesn\'t have the permission to send a message to" - f" #{message.channel} in server '{message.guild}'") - return - - # Redirect to the correct command - await COMMANDS[args[0]](message, args) - - -print(f"Current PID: {os.getpid()}") - -client.run(token) +bot = Bot( + "Discord Analyst", # name + "1.4", # version + alias="%", # respond to '|command' messages +) +bot.log_calls = True +bot.register_command( + "emotes", # command text (regex) + emotes.compute, # command function + "emotes: Emotes analysis", # short help + "```\n" + "* %emotes : Rank emotes by their usage\n" + "* %emotes @user : // for a specific user\n" + "* %emotes #channel : // for a specific channel\n" + "(Add more @user or #channel to be more selective)\n" + "```", +) +bot.start() diff --git a/help.py b/help.py deleted file mode 100644 index 2e371b2..0000000 --- a/help.py +++ /dev/null @@ -1,31 +0,0 @@ - - -async def compute(message, args): - """ - Computes the %help command - - :param message: message sent - :type message: discord.Message - :param args: arguments of the command - :type args: list[str] - """ - - # Select correct response to send - - response = "Discord Analyst commands:\n" \ - "```\n" \ - "%help (command) : Info on commands\n" \ - "%info : This bot info\n" \ - "%emotes : Emotes analysis\n" \ - "```" - - if len(args) > 1 and args[1] == "emotes": - response = "Emotes Analysis:\n" \ - "```\n" \ - "%emotes : Rank emotes by their usage\n" \ - "%emotes @user : // for a specific user\n" \ - "%emotes #channel : // for a specific channel\n" \ - "(Add more @user or #channel to be more selective)\n" \ - "```" - - await message.channel.send(response) diff --git a/requirements.txt b/requirements.txt old mode 100644 new mode 100755 index a41d83f..95a454b --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ discord.py python-dotenv +git+git://github.com/Klemek/miniscord.git