Skip to content

Commit

Permalink
Added possibility to reload config without restarting
Browse files Browse the repository at this point in the history
  • Loading branch information
yakMM committed May 1, 2021
1 parent c649fbd commit f71e737
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Added doc folder
- Accounts are now added to player objects before validation
- Bases can now be picked with pick command
- Config can now be reloaded with reload command

# v3.2:
- Added locking mechanic on reaction handler to avoid errors due to players spamming the reactions
Expand Down
5 changes: 5 additions & 0 deletions bot/cogs/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ async def reload(self, ctx, *args):
await loop.run_in_executor(None, db.get_all_elements, classes.Base, "static_bases")
await disp.BOT_RELOAD.send(ctx, "Bases")
return
if arg == "config":
await loop.run_in_executor(None, cfg.get_config, cfg.LAUNCH_STR)
await roles.update_rule_msg()
await disp.BOT_RELOAD.send(ctx, "Config")
return
await disp.WRONG_USAGE.send(ctx, ctx.command.name)

@commands.command(aliases=['rm'])
Expand Down
1 change: 1 addition & 0 deletions bot/display/embeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def admin_help(ctx):
value='`=channel (un)freeze` - Prevent users from typing in a channel\n'
'`=pog version` - Display current version and lock status\n'
'`=pog (un)lock` - Prevent users from interacting with the bot (but admins still can)\n'
'`=reload accounts`/`bases`/`weapons`/`config` - Reload specified element from the database\n'
'`=spam clear` - Clear the spam filter\n',
inline=False)
embed.add_field(name='Lobby commands',
Expand Down
11 changes: 2 additions & 9 deletions bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@
from match.classes.match import Match
from classes import Player, Base, Weapon


rules_msg = None # Will contain message object representing the rules message, global variable

log = logging.getLogger("pog_bot")

def _add_main_handlers(client):
Expand Down Expand Up @@ -140,7 +137,7 @@ async def on_raw_reaction_add(payload):
await disp.REG_RULES.send(ContextWrapper.channel(cfg.channels["register"]), payload.member.mention)
else:
await modules.roles.role_update(p)
await rules_msg.remove_reaction(payload.emoji, payload.member)
await modules.roles.rules_msg.remove_reaction(payload.emoji, payload.member)

# Reaction update handler (for accounts)
@client.event
Expand Down Expand Up @@ -186,11 +183,7 @@ async def on_ready():
modules.roles.init(client)

# fetch rule message, remove all reaction but the bot's
global rules_msg
rules_msg = await client.get_channel(cfg.channels["rules"]).fetch_message(cfg.general["rules_msg_id"])
await rules_msg.clear_reactions()
await sleep(0.2)
await rules_msg.add_reaction('✅')
await modules.roles.update_rule_msg()

# Update all players roles
for p in Player.get_all_players_list():
Expand Down
23 changes: 12 additions & 11 deletions bot/modules/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,13 @@ def get_config(launch_str):
_error_incorrect(key, 'General', file)

# Testing api key
#TODO CHANGE
skip_api_test = True
if not skip_api_test:
url = f"http://census.daybreakgames.com/s:{general['api_key']}/get/ps2:v2/faction"
j_data = loads(get(url).content)
if 'error' in j_data:
raise ConfigError(
f"Incorrect api key: {general['api_key']} in '{file}'")
# skip_api_test = True
# if not skip_api_test:
# url = f"http://census.daybreakgames.com/s:{general['api_key']}/get/ps2:v2/faction"
# j_data = loads(get(url).content)
# if 'error' in j_data:
# raise ConfigError(
# f"Incorrect api key: {general['api_key']} in '{file}'")

# Teamspeak section
_check_section(config, "Teamspeak", file)
Expand All @@ -242,6 +241,7 @@ def get_config(launch_str):

# Channels section
_check_section(config, "Channels", file)
channels_list.clear()

for key in channels:
try:
Expand Down Expand Up @@ -314,11 +314,12 @@ def get_config(launch_str):
VERSION = txt_str[3:-2]

# Map_Images section
_check_section(config, "Map_Images", file)
_check_section(config, "Base_Images", file)
base_images.clear()

for key in config['Map_Images'].keys():
for key in config['Base_Images'].keys():
try:
base_images[base_to_id[key]] = config['Map_Images'][key]
base_images[base_to_id[key]] = config['Base_Images'][key]
except KeyError:
raise ConfigError(f"Missing base '{key}' in 'base_to_id' dictionary in 'config.py'")

Expand Down
5 changes: 5 additions & 0 deletions bot/modules/reactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ async def clear_loop(msg):
_unlock_msg(msg.id)


async def add_reactions(msg, reactions):
for r in reactions:
await msg.add_reaction(r)


class ReactionHandler:
def __init__(self, rem_user_react=True, rem_bot_react=False, auto_destroy=False):
self.__f_dict = dict()
Expand Down
10 changes: 10 additions & 0 deletions bot/modules/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import modules.config as cfg

from discord import Status
from asyncio import sleep

_roles_dict = dict()
_guild = None
rules_msg = None


def init(client):
Expand All @@ -23,6 +25,14 @@ def is_admin(member):
return _roles_dict["admin"] in member.roles


async def update_rule_msg():
global rules_msg
rules_msg = await _guild.get_channel(cfg.channels["rules"]).fetch_message(cfg.general["rules_msg_id"])
await rules_msg.clear_reactions()
await sleep(0.2)
await rules_msg.add_reaction('✅')


def is_muted(member):
if member is None:
return False
Expand Down

0 comments on commit f71e737

Please sign in to comment.