add(command|warn): start dev of command warn

This commit is contained in:
Romain J 2019-09-19 01:48:52 +02:00
parent 764068ef22
commit 9274895226
8 changed files with 80 additions and 1086 deletions

View file

@ -54,4 +54,17 @@
- [x] info
- [ ] help
- [x] credits `new command`
---
# Cogs.ci commands
- [ ] ci (help?)
- [ ] ci show
- [ ] ci register
- [ ] ci delete
- [ ] ci update
- [ ] ci setconfig
- [ ] ci setos
- [ ] ci setcountry
- [ ] ci online_edit `renamed`, cause : `website down`
- [ ] ci list

6
bot.py
View file

@ -5,6 +5,7 @@ import traceback
from collections import deque
import aiohttp
import asyncpg
import discord
import git
from discord.ext import commands
@ -37,9 +38,9 @@ async def _prefix_callable(bot, message: discord.message) -> list:
class TuxBot(commands.AutoShardedBot):
__slots__ = ('uptime', 'config', 'session')
__slots__ = ('uptime', 'config', 'db', 'session')
def __init__(self, unload: list):
def __init__(self, unload: list, db: asyncpg.pool.Pool):
super().__init__(command_prefix=_prefix_callable, pm_help=None,
help_command=None, description=description,
help_attrs=dict(hidden=True),
@ -47,6 +48,7 @@ class TuxBot(commands.AutoShardedBot):
self.uptime: datetime = datetime.datetime.utcnow()
self.config = config
self.db = db
self._prev_events = deque(maxlen=10)
self.session = aiohttp.ClientSession(loop=self.loop)

View file

@ -3,6 +3,7 @@ from typing import Union
import discord
from discord.ext import commands
import humanize
from bot import TuxBot
from .utils.lang import Texts
@ -12,8 +13,9 @@ class Admin(commands.Cog):
def __init__(self, bot: TuxBot):
self.bot = bot
self.db = bot.db
async def cog_check(self, ctx: commands.Context):
async def cog_check(self, ctx: commands.Context) -> bool:
permissions: discord.Permissions = ctx.channel.permissions_for(
ctx.author)
@ -217,6 +219,49 @@ class Admin(commands.Cog):
await ctx.send(Texts('utils').get("Unable to find the message"),
delete_after=5)
"""---------------------------------------------------------------------"""
@commands.group(name='warn', aliases=['warns'])
async def _warn(self, ctx: commands.Context):
if ctx.invoked_subcommand is None:
query = """
SELECT user_id, reason, created_at FROM warns
WHERE created_at >= $1 AND server_id = $2
ORDER BY created_at
DESC LIMIT 10
"""
week_ago = datetime.datetime.now() - datetime.timedelta(weeks=6)
async with self.bot.db.acquire() as con:
warns = await con.fetch(query, week_ago, ctx.guild.id)
warns_list = ''
for warn in warns:
user_id = warn.get('user_id')
user = await self.bot.fetch_user(user_id)
reason = warn.get('reason')
ago = humanize.naturaldelta(
datetime.datetime.now() - warn.get('created_at')
)
warns_list += f"**{user}**: `{reason}` *({ago} ago)*\n"
e = discord.Embed(
title=f"{len(warns)} {Texts('admin').get('last warns')}: ",
description=warns_list
)
await ctx.send(embed=e)
@_warn.command(name='add', aliases=['new'])
async def _warn_new(self, ctx: commands.Context, member: discord.Member,
*, reason):
"""
todo: push in database
if warn > 2 for member:
todo: ask for confirmation to kick or ban
"""
def setup(bot: TuxBot):
bot.add_cog(Admin(bot))

File diff suppressed because it is too large Load diff

View file

@ -22,4 +22,7 @@ msgid "Unable to ban this user"
msgstr ""
msgid "Unable to kick this user"
msgstr ""
msgid "last warns"
msgstr ""

View file

@ -22,4 +22,7 @@ msgid "Unable to ban this user"
msgstr "Impossible de bannir cet utilisateur"
msgid "Unable to kick this user"
msgstr "Impossible d'expulser cet utilisateur"
msgstr "Impossible d'expulser cet utilisateur"
msgid "last warns"
msgstr "derniers avertissements"

View file

@ -4,7 +4,9 @@ import logging
import socket
import sys
import asyncpg
import click
import git
import requests
from bot import TuxBot
@ -50,17 +52,16 @@ def run_bot(unload: list = []):
print(Texts().get('Starting...'))
try:
pool = loop.run_until_complete(
db = loop.run_until_complete(
Table.create_pool(config.postgresql, command_timeout=60)
)
except socket.gaierror as e:
except socket.gaierror:
click.echo(Texts().get("Could not set up PostgreSQL..."),
file=sys.stderr)
log.exception(Texts().get("Could not set up PostgreSQL..."))
return
bot = TuxBot(unload)
bot.pool = pool
bot = TuxBot(unload, db)
bot.run()