-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_discord_client.py
121 lines (103 loc) · 4.74 KB
/
my_discord_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import glob
import logging
import os
import random
import time
import aiocron
import discord
from game_session_manager import GameSessionManager
from settings_loader import SettingLoader
logger = logging.getLogger('discord_bot')
class MyClient(discord.Client):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
logger.info('Start discord client')
self.settings = SettingLoader().settings
self.bot_id = os.getenv('DISCORD_BOT_ID', None)
async def on_ready(self):
logger.info("Logged in as '%s'" % self.user.name)
logger.info("Client id: %s" % self.user.id)
if self.settings.rank_enabled:
logger.info('Start cronjob with settings: {}'.format(self.settings.rank_cron_string))
cron = aiocron.crontab(self.settings.rank_cron_string, func=self.cronjob_print_rank_in_channel, start=False)
cron.start()
self.start_recording_activities()
async def cronjob_print_rank_in_channel(self):
logger.info("[Cronjob] cronjob_print_rank_in_channel start")
channel_id = self.settings.rank_channel_id
target_channel = self.get_channel(channel_id)
top_players = GameSessionManager.get_top_rank_last_week()
master_fapper_name = top_players[0][0]
embed = discord.Embed(title="Master fapper", color=0x109319)
embed.add_field(name="Master fapper of the week",
value=":trophy: **{}** :trophy:".format(master_fapper_name),
inline=False)
output = ("```" + "\n\n" + self.get_tabulate_rank(top_players) + "```")
embed.add_field(name="Fappers list", value=output,
inline=False)
await target_channel.send(embed=embed)
logger.info("[Cronjob] cronjob_print_rank_in_channel complete")
def no_luck(self):
"""
Return True following a percentage of chance
:return:
"""
chance = random.randint(1, 100)
if chance < self.settings.chance_to_troll:
return True
return False
async def on_voice_state_update(self, member, before, after):
"""
when a Member changes their voice state.
"""
if not self.settings.troll_member_when_join_channel:
return
if member.voice is not None:
# don't do it in AFK chan
if member.voice.afk:
logger.info("afk")
return
if member.id == self.bot_id: # do not troll the bot itself
return
if after.channel is not None:
logger.info("Member '%s' joined %s" % (member.name, after.channel.name))
# only troll if he was not already in this chan
if before.channel is None or before.channel.name != after.channel.name:
if self.no_luck():
logger.info("No luck, troll him!")
if after.channel not in (x.channel.name for x in self.voice_clients):
# connect to the channel
vc = await after.channel.connect()
# get random sound path
random_sound_path = self.get_random_sound_path(self.get_list_sound(self.settings.sounds_path))
vc.play(discord.FFmpegPCMAudio(random_sound_path), after=lambda e: print('done', e))
while vc.is_playing():
time.sleep(2)
logger.info("End playing")
# stop voice
await vc.disconnect()
async def on_member_update(self, before, after):
if before.id not in self.settings.rank_non_tracked_user_id:
if before.activity is None and after.activity is None:
return
logger.info("[User update] name: {}, id: {}".format(before.name, before.id))
GameSessionManager.handle_user_update(before, after)
@staticmethod
def get_tabulate_rank(sorted_player_list):
from tabulate import tabulate
headers = ["Player", "Hours"]
table = list()
for player in sorted_player_list:
user_line_list = [player[0], player[1]]
table.append(user_line_list)
return tabulate(table, headers, tablefmt="simple")
@staticmethod
def get_list_sound(path_to_list):
return glob.glob(path_to_list + os.sep + "*")
@staticmethod
def get_random_sound_path(list_sound):
return random.choice(list_sound)
def start_recording_activities(self):
for member in self.get_all_members():
if member.id not in self.settings.rank_non_tracked_user_id and member.activity is not None:
GameSessionManager.handle_user_update(before=member, after=member)