import traceback
import os
import logging
+from aioprometheus import Counter, Service, Gauge
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
-ch.setFormatter(logging.Formatter('|%(levelname)s|%(asctime)s|%(module)s| %(message)s'))
+ch.setFormatter(logging.Formatter('|%(levelname)s|%(asctime)s|%(module)s|%(lineno)d %(message)s'))
log.addHandler(ch)
from collections import defaultdict, OrderedDict
#exchange.publish(msg, routing_key=self.rabbit_config['exchange'])
+class HornProm(object):
+ def __init__(self):
+ self.horn_counter = Counter(
+ "grahbot_horn_count", "Number of horns submitted."
+ )
+ self.horn_voice = Gauge(
+ "grahbot_horn_voice_channels", "Channels actively joined by Grahbot"
+ )
+
+ async def inc_horn(self, data):
+ log.debug('Prom: Incrementing horn counter with data {}'.format(data))
+ self.horn_counter.inc(labels=data)
+
+ async def prom_voice_channel(self, channel, value):
+ log.debug('Prom: Setting voice channel {} to {}'.format(channel, value))
+ self.horn_voice.set(labels={'guild': channel.guild.name, 'channel': channel.name}, value=value)
+
+ async def prom_connect(self):
+ log.debug('Entered the prom_connect method')
+ svr = Service()
+ log.debug('Instantiated service')
+ svr.register(self.horn_counter)
+ svr.register(self.horn_voice)
+
+ await svr.start(addr="0.0.0.0", port=42069)
+ log.debug('Started prom server?')
+ while (True):
+ await asyncio.sleep(1)
+
class GrahDiscordException(Exception):
pass
-class GrahDiscordBot(discord.Client, HornClient):
+class GrahDiscordBot(discord.Client, HornClient, HornProm):
AUDIO_FILES = ['mp3', '.ogg']
CMD_MARKER = '!'
CMD_PREFIX = 'command_'
#self.horn_client = HornClient('config-piege.json')
HornClient.__init__(self, config)
self.rabbit = self.loop.create_task(self.rabbit_connect())
+
+ HornProm.__init__(self)
+ self.horn_prom = self.loop.create_task(self.prom_connect())
+
discord.Client.__init__(self, loop=self.loop, intents=intents)
self.loop_forever()
async def horn(self, obj, properties):
try:
log.info('Horn! {}'.format(obj))
+
# exclusive access on each horn or not?
if 'exclusive' in obj:
exclusive = obj['exclusive']
return None
else:
sample_name = '{}/{}'.format(self.config['airhorn_directory'], obj['sample_name'])
+ await self.inc_horn({'sample_name': obj['sample_name'], 'guild': discord.utils.get(self.guilds, id=obj['guild']).name, 'source': obj['source'], 'remote_ip': obj['remote_ip']})
+
except:
traceback.print_exc()
log.debug('Error object was: {}'.format(obj))
await self.msg_join_voice_channel(' '.join(message.content.split(' ')[1:]), guild=guild)
async def command_play(self, message, guild):
- log.info('Was instructed to play {}'.format(' '.join(message.content.split(' ')[1:])))
+ horn = ' '.join(message.content.split(' ')[1:])
+ log.info('Was instructed to play {}'.format(horn))
+ await self.inc_horn({'source': 'discord', 'guild': guild.name, 'sample_name': horn})
await self.msg_play_sound(name=' '.join(message.content.split(' ')[1:]), guild=guild)
async def command_list(self, message, guild):
async def on_voice_state_update(self, member, before, after):
if self.user.id == member.id:
log.debug('Something happened to me in voice! Member {} before {} after {}'.format(member, before, after))
+ if before.channel is not None:
+ await self.prom_voice_channel(before.channel, 0)
+ if after.channel is not None:
+ await self.prom_voice_channel(after.channel, 1)
if before.channel is not None and after.channel is not None and before.channel != after.channel:
log.debug('Before != after and channels differ, setting the VC appropriately')
# try to get the already established VC and resave