feat(monitoring): add http server for monitoring

This commit is contained in:
Romain J 2019-12-29 21:38:59 +01:00
parent 97980e96d1
commit c442fd55fe
4 changed files with 76 additions and 10 deletions

17
bot.py
View file

@ -30,6 +30,7 @@ l_extensions: List[str] = [
'cogs.poll', 'cogs.poll',
'cogs.help', 'cogs.help',
'jishaku', 'jishaku',
'cogs.monitoring'
] ]
@ -73,20 +74,21 @@ class TuxBot(commands.AutoShardedBot):
for extension in l_extensions: for extension in l_extensions:
try: try:
self.load_extension(extension)
print(Texts().get("Extension loaded successfully : ") print(Texts().get("Extension loaded successfully : ")
+ extension) + extension)
log.info(Texts().get("Extension loaded successfully : ") log.info(Texts().get("Extension loaded successfully : ")
+ extension) + extension)
self.load_extension(extension)
except Exception as e: except Exception as e:
print(Texts().get("Failed to load extension : ") print(Texts().get("Failed to load extension : ")
+ extension, file=sys.stderr) + extension, file=sys.stderr)
print(e)
log.error(Texts().get("Failed to load extension : ") log.error(Texts().get("Failed to load extension : ")
+ extension, exc_info=e) + extension, exc_info=e)
async def is_owner(self, user: discord.User) -> bool: async def is_owner(self, user: discord.User) -> bool:
return str(user.id) in self.config.get("permissions", "owners").split( return str(user.id) in self.config.get("permissions", "owners").split(
',') ', ')
async def on_socket_response(self, msg): async def on_socket_response(self, msg):
self._prev_events.append(msg) self._prev_events.append(msg)
@ -113,10 +115,10 @@ class TuxBot(commands.AutoShardedBot):
await self.invoke(ctx) await self.invoke(ctx)
async def on_message(self, message: discord.message): async def on_message(self, message: discord.message):
if message.author.bot \ if message.author.id in self.blacklist or (message.guild is not None and message.guild.id in self.blacklist):
or message.author.id in self.blacklist \ return
or (message.guild is not None
and message.guild.id in self.blacklist): if message.author.bot and message.author.id != int(self.config.get('bot', 'Tester')):
return return
await self.process_commands(message) await self.process_commands(message)
@ -147,7 +149,7 @@ class TuxBot(commands.AutoShardedBot):
logs_webhook = self.config["webhook"] logs_webhook = self.config["webhook"]
webhook = discord.Webhook.partial( webhook = discord.Webhook.partial(
id=logs_webhook.get('ID'), id=logs_webhook.get('ID'),
token=logs_webhook.get('Url'), token=logs_webhook.get('Token'),
adapter=discord.AsyncWebhookAdapter( adapter=discord.AsyncWebhookAdapter(
self.session self.session
) )
@ -195,6 +197,7 @@ if __name__ == "__main__":
print(Texts().get('Starting...')) print(Texts().get('Starting...'))
bot = TuxBot(Database(Config("./configs/config.cfg"))) bot = TuxBot(Database(Config("./configs/config.cfg")))
try: try:
with setup_logging(): with setup_logging():
bot.run() bot.run()

40
cogs/monitoring.py Normal file
View file

@ -0,0 +1,40 @@
import asyncio
import threading
from aiohttp import web
from discord.ext import commands
from bot import TuxBot
class Monitoring(commands.Cog):
def __init__(self):
self.app = web.Application()
t = threading.Thread(
target=self.run_server,
args=(self.aiohttp_server(),)
)
t.start()
def aiohttp_server(self):
async def hi(request):
return web.Response(text="I'm alive !")
self.app.add_routes([web.get('/', hi)])
runner = web.AppRunner(self.app)
return runner
@staticmethod
def run_server(runner):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(runner.setup())
site = web.TCPSite(runner, '0.0.0.', 3389)
loop.run_until_complete(site.start())
loop.run_forever()
def setup(bot: TuxBot):
bot.add_cog(Monitoring())

View file

@ -72,6 +72,26 @@ class Polls(commands.Cog):
await self.update_poll(poll.id) await self.update_poll(poll.id)
@commands.Cog.listener()
async def on_raw_reaction_remove(self, pld: discord.RawReactionActionEvent):
poll = self.get_poll(pld)
if poll:
choice = utils_emotes.get_index(pld.emoji.name)
responses = self.bot.database.session.query(Responses) \
.filter(
Responses.poll_id == poll.id,
Responses.user == pld.user_id,
Responses.choice == choice
)
if responses.count() != 0:
response = responses.first()
self.bot.database.session.delete(response)
self.bot.database.session.commit()
await self.update_poll(poll.id)
########################################################################### ###########################################################################
async def create_poll(self, ctx: commands.Context, poll: str, anonymous): async def create_poll(self, ctx: commands.Context, poll: str, anonymous):
@ -92,7 +112,7 @@ class Polls(commands.Cog):
) )
for i, response in enumerate(responses): for i, response in enumerate(responses):
e.add_field( e.add_field(
name=f"__```{emotes[i]} - {response.capitalize()}```__", name=f"__{emotes[i]}` - {response.capitalize()}`__",
value="**0** vote" value="**0** vote"
) )
e.set_footer(text=f"ID: #{poll_row.id}") e.set_footer(text=f"ID: #{poll_row.id}")

View file

@ -1,5 +1,5 @@
[280805240977227776] [280805240977227776]
prefixes = b1.|Imo07fZY9ogan7ank1n3UERg|b2. prefixes = test.
[303633056944881686] [303633056944881686]
prefixes = b1. prefixes = b1.
@ -8,5 +8,8 @@ prefixes = b1.
prefixes = b1. prefixes = b1.
[336642139381301249] [336642139381301249]
prefixes = prefixes = ba.
[274247231534792704]
prefixes = test.