Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 74 additions & 0 deletions src/commands/fun/word-leaderboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { SlashCommandBuilder } from "discord.js";
import { embedReplyPrimaryColor } from "../../helpers/embeds/embed-reply.js";
import { positionEmojis } from "../../helpers/format.js";
import { checkIfNotInGuild } from "../../helpers/command-validation/general.js";
import { logToFileAndDatabase } from "../../helpers/logger.js";
import { query } from "../../helpers/db.js";

const commandName = "word-leaderboard";

export default {
data: new SlashCommandBuilder()
.setName(commandName)
// Counts a specified word in the current server and sends back a leaderboard with the users who used that word the most.
.setDescription(
"Gives back a word count leaderboard with the top users who used that word the most."
)
.addStringOption((option) =>
option
.setName("word")
.setDescription("The word that will get counted.")
.setRequired(true)
.setMinLength(2)
.setMaxLength(12)
)
.setDMPermission(false),
async execute(interaction) {
await interaction.deferReply();

const guildCheck = checkIfNotInGuild(commandName, interaction);
if (guildCheck) {
return await replyAndLog(interaction, guildCheck);
}

const targetWord = interaction.options.getString("word");
const currentServerId = interaction.guild.id;

const usersQuery = await query(
`SELECT senderUserId, senderUserName, COUNT(*) as wordCount
FROM messageLog
WHERE serverId = ? AND messageContent LIKE ?
GROUP BY senderUserId
ORDER BY wordCount DESC
LIMIT 10`,
[currentServerId, `%${targetWord}%`]
);

let leaderboardContent = "";
for (let i = 0; i < usersQuery.length; i++) {
const userResult = usersQuery[i];
const emoji = positionEmojis[i + 1];

let username;
try {
const member = await interaction.guild.members.fetch(userResult.senderUserId);
username = member.user.username;
} catch (error) {
username = userResult.senderUserName || "Unknown username";
}

leaderboardContent += `${emoji} <@${userResult.senderUserId}> (${username}): **#${userResult.wordCount}**\n`;
}

const embedReply = embedReplyPrimaryColor(
`Word Leaderboard: "${targetWord}"`,
usersQuery.length !== 0
? `Leaderboard of users whose messages contained the word **${targetWord}** the most:\n\n${leaderboardContent}`
: `:x: No user has used the word **${targetWord}** in their messages so far.`,
interaction
);

await interaction.editReply({ embeds: [embedReply] });
await logToFileAndDatabase(interaction, JSON.stringify(embedReply.toJSON()));
},
};
1 change: 1 addition & 0 deletions src/data/commandData.csv
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@
193,cigany,fun,"Says ""fuj"" and sends a random pic of a ""person"" from a specific race."
194,cooldown,economy,Lets you check your or other users economy cooldowns for all or a specified economy command.
195,invite,utility,Gives you back a link for inviting the bot to any server where you have permissions to do so.
652,word-leaderboard,fun,Counts a specified word in the current server and sends back a leaderboard with the users who used that word the most through all their previous messages.
6 changes: 3 additions & 3 deletions src/events/scripts/messageCreate/messageLogging.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export const logToDB = async (message) => {
]
);

console.log(
`Logged message "${message.content}" to database from ${senderUserName} in ${serverName}.`
);
// console.log(
// `Logged message "${message.content}" to database from ${senderUserName} in ${serverName}.`
// );
}
}
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/darwin/darwinCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const addToCache = async (directVideoUrl, forumPostUrl, videoTitle) => {
try {
const exists = await isInCache(directVideoUrl);
if (exists) {
console.log(`URL already in cache: ${directVideoUrl}`);
// console.log(`URL already in cache: ${directVideoUrl}`);
return true;
}

Expand Down Expand Up @@ -76,7 +76,7 @@ export const addToCache = async (directVideoUrl, forumPostUrl, videoTitle) => {
export const isInCache = async (url) => {
try {
const videoId = extractVideoId(url);
console.log(`Checking "${url}" (ID: ${videoId}) in Darwin's cache`);
// console.log(`Checking "${url}" (ID: ${videoId}) in Darwin's cache`);

const result = await query(
"SELECT directVideoUrl FROM darwinCache WHERE directVideoUrl = ? OR videoId = ?",
Expand Down
13 changes: 13 additions & 0 deletions src/helpers/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,16 @@ export const formatRouletteColor = (color) => {
console.error("The color you've provided for the formatRouletteColor() function is invalid.");
}
};

export const positionEmojis = {
1: ":first_place:",
2: ":second_place:",
3: ":third_place:",
4: ":number_4:",
5: ":number_5:",
6: ":number_6:",
7: ":number_7:",
8: ":number_8:",
9: ":number_9:",
10: ":number_10:",
};
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ client.once(Events.ClientReady, (readyClient) => {

setInterval(() => {
if (darwinProcessRunning) {
console.log("Darwin process already running, skipping this execution");
// console.log("Darwin process already running, skipping this execution");
return;
}

Expand Down
14 changes: 0 additions & 14 deletions src/sql/darwinCache/migrate-to-new-schema.sql

This file was deleted.