Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
04051eeacc | ||
|
|
f33dc8ce37 | ||
|
|
306f983c8e | ||
|
|
9e9d616814 | ||
|
|
5a09beea31 | ||
|
|
8c2b3fa487 | ||
|
|
62e89960f7 | ||
|
|
e2b0c0ea89 |
@@ -2,9 +2,6 @@
|
|||||||
Defines cog for guild listeners. Contains functions for various guild events.
|
Defines cog for guild listeners. Contains functions for various guild events.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
|
||||||
import json
|
|
||||||
|
|
||||||
from nextcord.ext import commands
|
from nextcord.ext import commands
|
||||||
import nextcord
|
import nextcord
|
||||||
|
|
||||||
@@ -20,7 +17,6 @@ class GuildListener(commands.Cog, name="Guild Listener"):
|
|||||||
@commands.Cog.listener()
|
@commands.Cog.listener()
|
||||||
async def on_guild_join(self, guild: nextcord.Guild):
|
async def on_guild_join(self, guild: nextcord.Guild):
|
||||||
"""Listener for Guild Join. Creates skeleton for new guild joined and saves it."""
|
"""Listener for Guild Join. Creates skeleton for new guild joined and saves it."""
|
||||||
# turning guild id into string because json doesn't support ints as object keys.
|
|
||||||
highscores_data.add_new_server(guild.id)
|
highscores_data.add_new_server(guild.id)
|
||||||
|
|
||||||
welcome_message: str = "Hello, I keep track of Highscores for you!"
|
welcome_message: str = "Hello, I keep track of Highscores for you!"
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
"""
|
||||||
|
Functions for controlling the bot's presence.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import random
|
||||||
|
|
||||||
|
from nextcord.ext import commands
|
||||||
|
from nextcord.ext import tasks
|
||||||
|
import nextcord
|
||||||
|
|
||||||
|
|
||||||
|
class BotPresence(commands.Cog, name="Bot Presence"):
|
||||||
|
"""Class for bot presence functions."""
|
||||||
|
|
||||||
|
def __init__(self, bot: commands.Bot):
|
||||||
|
self.bot = bot
|
||||||
|
|
||||||
|
@commands.Cog.listener()
|
||||||
|
async def on_guild_join(self):
|
||||||
|
"""Listener for Guild Join. Updated presence on guild join."""
|
||||||
|
await self.change_presence()
|
||||||
|
|
||||||
|
@commands.Cog.listener()
|
||||||
|
async def on_ready(self):
|
||||||
|
"""Starts the loop when bot is ready."""
|
||||||
|
await self.bot.wait_until_ready()
|
||||||
|
# pylint doesn't recognize that periodic_change_presense is subclassed from tasks.loop.
|
||||||
|
self.periodic_change_presence.start() # pylint: disable=no-member
|
||||||
|
|
||||||
|
@tasks.loop(minutes=2)
|
||||||
|
async def periodic_change_presence(self):
|
||||||
|
"""Loops every 2 minutes and changes presence."""
|
||||||
|
await self.change_presence()
|
||||||
|
|
||||||
|
async def change_presence(self):
|
||||||
|
"""Randomly chooses a presence and updates bot's presence."""
|
||||||
|
statuses = [
|
||||||
|
f"?help | {len(self.bot.guilds)} servers",
|
||||||
|
f"Vuln Pls. | {len(self.bot.guilds)} servers",
|
||||||
|
f"All your base are belong to us. | {len(self.bot.guilds)} servers",
|
||||||
|
f"Double plus good. | {len(self.bot.guilds)} servers",
|
||||||
|
]
|
||||||
|
|
||||||
|
status = random.choice(statuses)
|
||||||
|
|
||||||
|
await self.bot.change_presence(activity=nextcord.Game(status))
|
||||||
|
|
||||||
|
|
||||||
|
# This function will be called when this extension is loaded.
|
||||||
|
# It is necessary to add these functions to the bot.
|
||||||
|
def setup(bot: commands.Bot):
|
||||||
|
"""Adds function to bot"""
|
||||||
|
bot.add_cog(BotPresence(bot))
|
||||||
+6
-6
@@ -4,6 +4,7 @@ main file for pvm-highscores bot
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
import nextcord
|
import nextcord
|
||||||
from nextcord.ext import commands
|
from nextcord.ext import commands
|
||||||
@@ -32,6 +33,11 @@ submission_objects: dict = {}
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Main function for bot."""
|
"""Main function for bot."""
|
||||||
|
min_python_version = (3, 7)
|
||||||
|
if sys.version_info < min_python_version:
|
||||||
|
print("ERROR: Python version must be >= 3.7")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
# Allows privledged intents for monitoring members joining, roles editing, and role assignments
|
# Allows privledged intents for monitoring members joining, roles editing, and role assignments
|
||||||
# These need to be enabled in the developer portal as well
|
# These need to be enabled in the developer portal as well
|
||||||
intents = nextcord.Intents.default()
|
intents = nextcord.Intents.default()
|
||||||
@@ -45,15 +51,9 @@ def main():
|
|||||||
# To enable the members priveleged intent:
|
# To enable the members priveleged intent:
|
||||||
# intents.members = True
|
# intents.members = True
|
||||||
|
|
||||||
# Set custom status to "Listening to ?help"
|
|
||||||
activity = nextcord.Activity(
|
|
||||||
type=nextcord.ActivityType.listening, name=f"{config.BOT_PREFIX}help"
|
|
||||||
)
|
|
||||||
|
|
||||||
bot = commands.Bot(
|
bot = commands.Bot(
|
||||||
commands.when_mentioned_or(config.BOT_PREFIX),
|
commands.when_mentioned_or(config.BOT_PREFIX),
|
||||||
intents=intents,
|
intents=intents,
|
||||||
activity=activity,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Get the modules of all cogs whose directory structure is ./cogs/<module_name>
|
# Get the modules of all cogs whose directory structure is ./cogs/<module_name>
|
||||||
|
|||||||
@@ -32,8 +32,8 @@ class HighscoresData:
|
|||||||
skeleton["tables"][boss] = {"message_id": 0}
|
skeleton["tables"][boss] = {"message_id": 0}
|
||||||
boss_categories = {}
|
boss_categories = {}
|
||||||
for category in categories:
|
for category in categories:
|
||||||
rank_list = []
|
scores_list = {}
|
||||||
boss_categories[category] = rank_list
|
boss_categories[category] = scores_list
|
||||||
skeleton["tables"][boss]["categories"] = boss_categories
|
skeleton["tables"][boss]["categories"] = boss_categories
|
||||||
|
|
||||||
self._data[str(guild_id)] = skeleton
|
self._data[str(guild_id)] = skeleton
|
||||||
@@ -66,40 +66,17 @@ class HighscoresData:
|
|||||||
minutes = score.split(":")[0]
|
minutes = score.split(":")[0]
|
||||||
seconds = score.split(":")[1]
|
seconds = score.split(":")[1]
|
||||||
score_seconds = int(minutes) * 60 + float(seconds)
|
score_seconds = int(minutes) * 60 + float(seconds)
|
||||||
score_tuple = (user, score, score_seconds)
|
score_list = [score, score_seconds]
|
||||||
sort_index = 2
|
|
||||||
else:
|
|
||||||
score_tuple = (user, int(score))
|
|
||||||
sort_index = 1
|
sort_index = 1
|
||||||
|
|
||||||
# if user is in an existing tuple, delete it.
|
|
||||||
users_scores: "list[tuple]" = [score_tuple]
|
|
||||||
for tup in scores:
|
|
||||||
if user == tup[0]:
|
|
||||||
# Add tup to users_scores, remove from scores
|
|
||||||
users_scores.append(tup)
|
|
||||||
|
|
||||||
# sort users_scores
|
|
||||||
users_scores.sort(
|
|
||||||
key=lambda x: x[sort_index],
|
|
||||||
reverse=not ascending,
|
|
||||||
)
|
|
||||||
|
|
||||||
for index, tup in enumerate(users_scores):
|
|
||||||
if index == 0:
|
|
||||||
# first score is the best score.
|
|
||||||
if tup not in scores:
|
|
||||||
scores.append(tup)
|
|
||||||
else:
|
else:
|
||||||
# discard all other scores from scores list.
|
score_list = [int(score)]
|
||||||
if tup in scores:
|
sort_index = 0
|
||||||
scores.remove(tup)
|
|
||||||
|
|
||||||
# sort scores.
|
# Will not ensure new score is better than old one. Allows for correcting mistakes.
|
||||||
scores.sort(
|
|
||||||
key=lambda x: x[sort_index],
|
scores[user] = score_list
|
||||||
reverse=not ascending,
|
|
||||||
)
|
scores = dict(sorted(scores.items(), key=lambda x: x[1][sort_index], reverse=not ascending))
|
||||||
|
|
||||||
self._data[str(guild_id)]["tables"][boss_name]["categories"][category] = scores
|
self._data[str(guild_id)]["tables"][boss_name]["categories"][category] = scores
|
||||||
save_highscores_data(self._data)
|
save_highscores_data(self._data)
|
||||||
|
|||||||
+10
-9
@@ -17,27 +17,28 @@ def format_highscore_message(boss_name: str, guild_id: int):
|
|||||||
ret_string = f"**{boss_name}**\n```"
|
ret_string = f"**{boss_name}**\n```"
|
||||||
|
|
||||||
# Gather all categories and add to string
|
# Gather all categories and add to string
|
||||||
for key, value in highscores_data.get_categories_data(guild_id, boss_name):
|
for category_name in highscores_data.get_table_categories(guild_id, boss_name):
|
||||||
ret_string += f"{key}".ljust(26, " ")
|
ret_string += f"{category_name}".ljust(26, " ")
|
||||||
ret_string += "\n"
|
ret_string += "\n"
|
||||||
|
|
||||||
highscore_size = highscores_data.get_config_highscore_size()
|
highscore_size = highscores_data.get_config_highscore_size()
|
||||||
|
|
||||||
# list rankings in a single line. Start with first place for each category and then continue
|
# list rankings in a single line. Start with first place for each category and then continue
|
||||||
for i in range(highscore_size):
|
for i in range(highscore_size):
|
||||||
for key, value in highscores_data.get_categories_data(guild_id, boss_name):
|
for category_name, category_data in highscores_data.get_categories_data(
|
||||||
if len(value) > i:
|
guild_id, boss_name
|
||||||
score_pair = value[i]
|
):
|
||||||
|
if len(category_data) > i:
|
||||||
# pad username to max length.
|
# pad username to max length.
|
||||||
username: str = score_pair[0].ljust(
|
username: str = list(category_data.keys())[i].ljust(
|
||||||
highscores_data.get_guild_username_length(guild_id), " "
|
highscores_data.get_guild_username_length(guild_id), " "
|
||||||
)
|
)
|
||||||
score: str = str(score_pair[1])
|
# turns category data value into a string. Very janky.
|
||||||
|
score: str = str(list(category_data.values())[i][0])
|
||||||
|
|
||||||
# pad score to correct length
|
# pad score to correct length
|
||||||
# MM:SS.ms is 7 characters long
|
# MM:SS.ms is 7 characters long
|
||||||
if highscores_data.get_config_time_record(key):
|
if highscores_data.get_config_time_record(category_name):
|
||||||
# if there are single digit minutes, add a space for formatting.
|
# if there are single digit minutes, add a space for formatting.
|
||||||
if score.find(":") and len(score.split(":", maxsplit=1)[0]) < 2:
|
if score.find(":") and len(score.split(":", maxsplit=1)[0]) < 2:
|
||||||
score = " " + score
|
score = " " + score
|
||||||
|
|||||||
Reference in New Issue
Block a user