Skip to content

Commit

Permalink
Minor fixes/changes
Browse files Browse the repository at this point in the history
  • Loading branch information
yakMM committed Apr 9, 2021
1 parent af90231 commit 79b5b5a
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 42 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# v3.1:
- Fixed some issues with sub command
- Added base image as thumbmail in the match info embed
- Added base image as thumbnail in the match info embed
- Notify players will no longer be pinged if they are already in a match
- Overhauled team ready section:
- Added reactions for the =ready command
Expand All @@ -12,6 +12,10 @@
- Now tracking per-class stats internally
- Added match plugins
- Added ts3 interaction as a plugin
- Fixed some problems in the ts3 bot handling
- TS3 channels ids are not in the config file
- Separated =lobby get from =lobby save
- Players name are now displayed in lobby lists

# v3.0:
- Code revamp:
Expand Down
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ api_key = Daybreak_Registered_Service_ID
```

### Teamspeak integration
The bot used for Teamspeak audio integration is Splamy's [TS3AudioBot](https://github.com/Splamy/TS3AudioBot)
The bot used for Teamspeak audio integration is Splamy's [TS3AudioBot](https://github.com/yakMM/TS3AudioBot).
This bot works on the dotnet runtime and can be built and installed following the readme available in TS3AudioBot github's repo.
As of now, the version `0.12.1` of TS3AudioBot is used.
As of now, a fork of version `0.12.1` is used.

#### TS3-bot folder structure
The structure of the TS3AudioBot folder is the following:
Expand Down Expand Up @@ -136,6 +136,11 @@ bots_path = "bots"
# Path for audio files
media = { path = "audio" }
[bot.audio]
# Activate subscription-based whispers
send_mode = "!whisper subscription"
[bot.connect]
# TS3 default connect information
address = Your_TS3_Url
Expand All @@ -152,14 +157,19 @@ Additionally, add access to all commands for localhost in the `rights.toml` file
```

#### Configuring individual bots
Each individual bot is also to be configured (in each `bot.toml` file). The main parameter to modify is `send_mode`.
This parameter is needed for the bot to send whisper broadcast to the match channels.
Each individual bot can also be configured, in each `bot.toml` files. This allows for example to change the name of each individual bot.
```buildoutcfg
[audio]
# Example if your match, team1 and team2 channels IDs are 1, 2 and 3
send_mode = "!xecute (!whisper subscription) (!subscribe channel 1) (!subscribe channel 2) (!subscribe channel 3)"
[connect]
# Client nickname when connecting.
name = "POG_3"
```

#### Setting up TS3 channels IDs
THe channel IDs can be set up in the configuration file:




### Populating the collections
The file `scripts.py` contains two functions called `push_accounts()` and `get_all_maps_from_api()`.
Expand Down
4 changes: 4 additions & 0 deletions bot/classes/scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ async def update_stats(self):
def match(self):
return self.__team.match

@property
def mention(self):
return f"<@{self.__id}>"

@property
def name(self):
return self.__name
Expand Down
6 changes: 5 additions & 1 deletion bot/cogs/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,13 @@ async def lobby(self, ctx, *args):
pass
await disp.LB_QUEUE.send(ctx, names_in_lobby=lobby.get_all_names_in_lobby())
return
if len(args) > 0 and args[0] == "get":
if len(args) > 0 and args[0] == "save":
lb = lobby.get_all_ids_in_lobby()
await db.async_db_call(db.set_field, "restart_data", 0, {"last_lobby": lb})
await disp.LB_SAVE.send(ctx)
return
if len(args) > 0 and args[0] == "get":
lb = lobby.get_all_ids_in_lobby()
await disp.LB_GET.send(ctx, " ".join([str(p_id) for p_id in lb]))
return
await disp.WRONG_USAGE.send(ctx, ctx.command.name)
Expand Down
3 changes: 2 additions & 1 deletion bot/config_template.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ rules_msg = # id of the rule message users have to validate
url = # Teamspeak bot webapi url
config_help = # Url of the image showing how to join the Ts3 server
lobby_id = # Lobby channel id
matches = # Matches channel ids (example: 1/2/3,4/5/6) (matches separated by commas, channels by slashes, no spaces)

[Channels]
lobby = # id of lobby channel
register = # id of register channel
matches = # id of matches channels (seperated by commas without spaces)
matches = # id of matches channels (separated by commas without spaces)
results = # id of lobby channel
rules = # id of lobby channel
staff = # id of staff channel
Expand Down
6 changes: 3 additions & 3 deletions bot/display/embeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def lobby_help(ctx):
embed.add_field(name="Staff commands",
value='`=clear` - Clear the lobby\n'
'`=remove @player` - Remove player from lobby\n'
'`=channel freeze`/`unfreeze` - Prevent / Allow players to send messages\n',
'`=channel freeze`/`unfreeze` - Prevent / Allow players to send messages\n'
'`=lobby save`/`get`/`restore` - Will save, get or restore the lobby from player IDs',
inline=False)
return embed

Expand All @@ -70,8 +71,7 @@ def admin_help(ctx):
embed.add_field(name='Lobby commands',
value='`=remove @player` - Remove the player from queue\n'
'`=clear` - Clear queue\n'
'`=lobby get` - Save the lobby state in the db\n'
'`=lobby restore id id ...` - Re-add all the users back to the lobby',
'`=lobby save`/`get`/`restore` - Will save, get or restore the lobby from player IDs',
inline=False)
embed.add_field(name='Player Management commands',
value='`=rename @player New Name` - Rename a player within the system\n'
Expand Down
6 changes: 4 additions & 2 deletions bot/display/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class AllStrings(Enum):
LB_CLEARED = Message("Lobby has been cleared!", embed=embeds.lobby_list)
LB_EMPTY = Message("Lobby is already empty!")
LB_NOTIFY = Message("{} queue is almost full, join to start a match!")
LB_GET = Message("Lobby status saved, will be restored on next restart!")
LB_GET = Message("Restore the lobby with `=lobby restore {}`")
LB_SAVE = Message("Lobby status saved, will be restored on next restart!")
LB_REFRESHED = Message("You have reset your queue timeout!")

PK_OVER = Message("The teams are already made. You can't pick!")
Expand Down Expand Up @@ -96,6 +97,7 @@ class AllStrings(Enum):
API_ERROR = Message("Could not reach Planetside2 API, try again later!")
API_READY_ERROR = Message("Could not reach Planetside2 API, player online check ignored!", ping=False)
API_SCORE_ERROR = Message("Match {}, round {}: Could not reach Planetside2 API, no scores for this round!")
PUBLISH_ERROR = Message("Match {}, round {}: Could not publish score image, no scores for this round!")
GLOBAL_INFO = Message("Here is what's going on in POG at the moment:", embed=embeds.global_info)
CHECK_ACCOUNT = Message("Your account password may have been flipped!\n"
"Re-register in <#{}> to confirm you still have access to it!", embed=embeds.flip_accounts)
Expand Down Expand Up @@ -142,7 +144,7 @@ class AllStrings(Enum):
MATCH_INIT = Message("{}\nMatch is ready, starting team selection...")
MATCH_SHOW_PICKS = Message("Captains have been selected, {} choose a player", embed=embeds.team_update, ping=False)
MATCH_BASE_AUTO = Message("Match will be on **{}**", ping=False)
MATCH_CONFIRM = Message("{} {} Type `=ready` when your team is inside their sunderer, ready to start",
MATCH_CONFIRM = Message("{} {} Type `=ready` or react below when your team is inside their sunderer, ready to start",
embed=embeds.team_update)
MATCH_TEAM_READY = Message("{} is now ready!", embed=embeds.team_update)
MATCH_TEAM_UNREADY = Message("{} is no longer ready!", embed=embeds.team_update)
Expand Down
15 changes: 8 additions & 7 deletions bot/lib/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ def __init__(self, coro, seconds=0, hours=0, minutes=0, delay=0, count=1, reconn
self._current_loop = 0
self._task = None
self._injected = None
self._valid_exception = (
OSError,
discord.GatewayNotFound,
discord.ConnectionClosed,
aiohttp.ClientError,
asyncio.TimeoutError,
)
self._valid_exception = ()
# self._valid_exception = (
# OSError,
# discord.GatewayNotFound,
# discord.ConnectionClosed,
# aiohttp.ClientError,
# asyncio.TimeoutError,
# )

self._before_loop = None
self._after_loop = None
Expand Down
2 changes: 1 addition & 1 deletion bot/match/plugins/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ def on_match_over(self):

async def clean(self):
self.data["cleaning"] = timestamp_now()
await db.async_db_call(db.set_element, "match_logs", self.match.id, self.data)
# await db.async_db_call(db.set_element, "match_logs", self.match.id, self.data)
46 changes: 32 additions & 14 deletions bot/match/plugins/ts3_interface.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import modules.config as cfg
from modules.asynchttp import request_code as http_request
from lib.tasks import loop
from lib.tasks import loop, Loop

from asyncio import sleep
from logging import getLogger
Expand All @@ -21,8 +21,9 @@ def __init__(self, match):
self.lobby = False

def on_match_launching(self):
Loop(coro=configure, count=1).start(self.num)
audio_string = f"drop_match_{self.num}_picks"
_TaskAudio(self).task_audio.start(audio_string, lobby=True)
_TaskAudio(self).task_audio.start(audio_string, lobby=True, wait=0)

def on_captain_selected(self):
_TaskAudio(self).task_audio.start("select_teams", lobby=False)
Expand All @@ -35,28 +36,32 @@ def on_faction_pick(self, team):
_TaskAudio(self).task_audio.start(audio_string)

def on_factions_picked(self):
_TaskAudio(self).task_audio.start("select_base")
if not self.match.base:
_TaskAudio(self).task_audio.start("select_base")

def on_base_selected(self, base):
_TaskAudio(self).task_audio.start("base_selected")
_TaskAudio(self).task_audio.start(f'base_{cfg.id_to_base[base.id]}')
_TaskAudio(self).task_audio.start("type_ready")
_TaskAudio(self).task_audio.start("base_selected", wait=0)
_TaskAudio(self).task_audio.start(f'base_{cfg.id_to_base[base.id]}', wait=1)
_TaskAudio(self).task_audio.start("type_ready", wait=2)

def on_team_ready(self, team):
audio_string = f"team_{team.id + 1}_ready"
_TaskAudio(self).task_audio.start(audio_string)

def on_match_starting(self):
# Timing tested
_TaskAudio(self).task_audio.start("30s")
_TaskAudio(self).task_audio.start("30s", wait=0)
_TaskAudio(self).task_audio.start("10s", wait=20)
_TaskAudio(self).task_audio.start("5s", wait=25)

def on_round_over(self):
_TaskAudio(self).task_audio.start("round_over")
if self.match.round_no == 1:
_TaskAudio(self).task_audio.start("switch_sides")
_TaskAudio(self).task_audio.start("type_ready")
_TaskAudio(self).task_audio.start("switch_sides", wait=0)
_TaskAudio(self).task_audio.start("type_ready", wait=1)

async def clean(self):
self.lobby = False


class _TaskAudio:
Expand All @@ -65,8 +70,8 @@ def __init__(self, bot):
self.bot = bot

@loop(count=1)
async def task_audio(self, string, lobby=False, wait=0):
if wait != 0:
async def task_audio(self, string, lobby=False, wait=-1):
if wait >= 0:
await sleep(wait)
await self.__lobby(lobby)
url = f'{cfg.ts["url"]}/api/bot/template/{self.bot.num}(/xecute(/add/{string}.mp3)(/play))'
Expand All @@ -85,6 +90,19 @@ async def __lobby(self, bl):


async def _send_url(url):
code = await http_request(url)
if code != 204:
log.warning(f'TS3Bot API: Received code {code} on {url}')
try:
code = await http_request(url)
if code != 204:
log.warning(f'TS3Bot API: Received code {code} on {url}')
except Exception as e:
log.warning(f"Couldn't join TS3 bot on {url}\n{e}")


async def configure(num):
channels_str = "/".join(str(c_id) for c_id in cfg.ts["matches"][num - 1])
url = f'{cfg.ts["url"]}/api/bot/template/{num}(/subscribe/channel/{channels_str})'
try:
await _send_url(url)
except Exception as e:
log.warning(f"Couldn't configure TS3 bot on {url}\n{e}")

8 changes: 7 additions & 1 deletion bot/match/processes/match_playing.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ async def on_match_over(self):
await disp.MATCH_ROUND_OVER.send(self.match.channel, *player_pings, self.match.round_no)
try:
await census.process_score(self.match.data, self.match.last_start_stamp, self.match.channel)
await i_maker.publish_match_image(self.match)
try:
await i_maker.publish_match_image(self.match)
except Exception as e:
# Should not happen
log.error(f"Error in publish_match_image : {e}")
await disp.PUBLISH_ERROR.send(ContextWrapper.channel(cfg.channels["results"]), self.match.id,
self.match.round_no)
except ApiNotReachable as e:
log.error(f"ApiNotReachable caught when processing scores : {e.url}")
await disp.API_SCORE_ERROR.send(ContextWrapper.channel(cfg.channels["results"]), self.match.id,
Expand Down
13 changes: 11 additions & 2 deletions bot/modules/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ def __init__(self, msg):
ts = {
"url": "",
"config_help": "",
"lobby_id": 0
"lobby_id": 0,
"matches": list()
}

# Channels
Expand Down Expand Up @@ -220,7 +221,15 @@ def get_config(launch_str):

for key in ts:
try:
if isinstance(ts[key], int):
if key == "matches":
tmp = config['Teamspeak'][key].split(',')
ts[key].clear()
for m in tmp:
ts[key].append(list())
c_id = m.split('/')
for val in c_id:
ts[key][-1].append(int(val))
elif isinstance(ts[key], int):
ts[key] = int(config['Teamspeak'][key])
else:
ts[key] = config['Teamspeak'][key]
Expand Down
4 changes: 2 additions & 2 deletions bot/modules/lobby.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def get_lobby_len():


def get_all_names_in_lobby():
names = [p.mention for p in _lobby_list]
names = [f"{p.mention} ({p.name})" for p in _lobby_list]
return names


Expand All @@ -116,7 +116,7 @@ def remove_from_lobby(player):


def _on_match_free():
_auto_ping.already = True
_auto_ping.already = False
if len(_lobby_list) == cfg.general["lobby_size"]:
_start_match_from_full_lobby()

Expand Down

0 comments on commit 79b5b5a

Please sign in to comment.