transition to miniscord

This commit is contained in:
klemek
2021-01-06 12:09:10 +01:00
parent 84682c015e
commit 9f7ffff43c
4 changed files with 22 additions and 121 deletions
Regular → Executable
+1
View File
@@ -3,3 +3,4 @@ venv
__pycache__ __pycache__
.env .env
error_* error_*
*.log
Regular → Executable
+18 -88
View File
@@ -1,92 +1,22 @@
import os from miniscord import Bot
import discord
from datetime import datetime
from dotenv import load_dotenv
# Custom libs
import emotes import emotes
import help
from utils import debug
VERSION = "1.3" bot = Bot(
t0 = datetime.now() "Discord Analyst", # name
"1.4", # version
# Loading token alias="%", # respond to '|command' messages
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 bot.log_calls = True
print(f'{client.user} v{VERSION} has connected to Discord\nto the following guilds:') bot.register_command(
for guild in client.guilds: "emotes", # command text (regex)
print(f'- {guild.name}(id: {guild.id})') emotes.compute, # command function
"emotes: Emotes analysis", # short help
"```\n"
@client.event "* %emotes : Rank emotes by their usage\n"
async def on_message(message): "* %emotes @user : // for a specific user\n"
""" "* %emotes #channel : // for a specific channel\n"
Called when a message is sent to any channel on any guild "(Add more @user or #channel to be more selective)\n"
"```",
:param message: message sent )
:type message: discord.Message bot.start()
"""
# 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)
-31
View File
@@ -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)
Regular → Executable
+1
View File
@@ -1,2 +1,3 @@
discord.py discord.py
python-dotenv python-dotenv
git+git://github.com/Klemek/miniscord.git