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: 22 additions & 0 deletions default-locales.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,28 @@
"reload-failed-message": "**FAILED**\n```%r```\n**Please read your log to find more information**\nThe bot will kill itself now, bye :wave:",
"command-description": "Reloads the configuration"
},
"hunt-the-code": {
"admin-command-description": "Manage the current Code-Hunt",
"create-code-description": "Create a new code for the current code-hunt",
"display-name-description": "Name of the code that will be displayed to user when they redeem the code",
"code-description": "Set the code that will be used to redeem it (default: randomly generated)",
"code-created": "Code \"%displayName\" successfully created: \"%code\"",
"error-creating-code": "Error creating code \"{{displayName}}\". Maybe the entered code is already in the database?",
"successful-reset": "Successfully ended the current Code-Hunt-Game - [here](%url)'s your report - save the URL if you want to access it later.",
"end-description": "Ends the current Code-Hunt (will clear users and codes and generates a report)",
"command-description": "Redeem or see data about the current Code-Hunt",
"redeem-description": "Redeem a code you found",
"code-redeem-description": "The code you want to redeem",
"leaderboard-description": "See the current leaderboard",
"profile-description": "See your current count of found codes",
"no-codes-found": "No codes redeemed yet ):",
"no-users": "No users redeemed codes yet ):",
"report-header": "Report for the Hunt-The-Code game on %s",
"user-header": "Participating users",
"code-header": "Codes",
"report-description": "Generates a report",
"report": "You can find the report [here](%url)."
},
"config": {
"checking-config": "Checking configurations...",
"done-with-checking": "Done with checking. Out of %totalModules modules, %enabled were enabled, %configDisabled were disabled because their configuration was wrong.",
Expand Down
120 changes: 120 additions & 0 deletions modules/hunt-the-code/commands/hunt-the-code-admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
const {localize} = require('../../../src/functions/localize');
const {randomString, postToSCNetworkPaste} = require('../../../src/functions/helpers');

module.exports.subcommands = {
'create-code': function (interaction) {
interaction.client.models['hunt-the-code']['Code'].create({
code: (interaction.options.getString('code') || (randomString(3) + '-' + randomString(3) + '-' + randomString(3))).toUpperCase(),
displayName: interaction.options.getString('display-name')
}).then((codeObject) => {
interaction.reply({
ephemeral: true,
content: '✅ ' + localize('hunt-the-code', 'code-created', {
displayName: interaction.options.getString('display-name'),
code: codeObject.code
})
});
}).catch(() => {
interaction.reply({
ephemeral: true,
content: '⚠ ' + localize('hunt-the-code', 'error-creating-code', {displayName: interaction.options.getString('display-name')})
});
});
},
'end': async function (interaction) {
await interaction.deferReply({ephemeral: true});
const url = await generateReport(interaction.client);
await interaction.client.models['hunt-the-code']['Code'].destroy({
truncate: true
});
await interaction.client.models['hunt-the-code']['User'].destroy({
truncate: true
});
await interaction.editReply({
content: '✅ ' + localize('hunt-the-code', 'successful-reset', {url})
});
},
'report': async function (interaction) {
await interaction.deferReply({ephemeral: true});
const url = await generateReport(interaction.client);
await interaction.editReply({
content: localize('hunt-the-code', 'report', {url})
});
}
};

/**
* Generate a report of the current Code-Hunt-Session
* @param {Client} client Client
* @returns {Promise<string>} URL to Report
*/
async function generateReport(client) {
let reportString = `# ${localize('hunt-the-code', 'report-header', {s: client.guild.name})}\n`;
const codes = await client.models['hunt-the-code']['Code'].findAll({
order: [
['foundCount', 'DESC']
]
});
const users = await client.models['hunt-the-code']['User'].findAll({
order: [
['foundCount', 'DESC']
]
});
reportString = reportString + `\n## ${localize('hunt-the-code', 'user-header')}\n| Rank | Tag | ID | Amount found | Codes |\n| --- | --- | --- | --- | --- |\n`;
for (const i in users) {
const user = users[i];
const u = await client.users.fetch(user.id);
reportString = reportString + `| ${parseInt(i) + 1}. | ${u.tag} | ${u.id} | ${user.foundCount} | ${user.foundCodes.join(', ')} |\n`;
}
reportString = reportString + `\n## ${localize('hunt-the-code', 'code-header')}\n| Rank | Code | Display-Name | Times found |\n| --- | --- | --- | --- |\n`;
for (const i in codes) {
const code = codes[i];
reportString = reportString + `| ${parseInt(i) + 1}. | ${code.code} | ${code.displayName} | ${code.foundCount} |\n`;
}
reportString = reportString + `\n<br><br><br><hr>Generated at ${new Date().toLocaleString(client.locale)}.`;
return await postToSCNetworkPaste(reportString, {
expire: '1month',
burnafterreading: 0,
opendiscussion: 1,
textformat: 'markdown',
output: 'text',
compression: 'zlib'
});
}

module.exports.config = {
name: 'hunt-the-code-admin',
description: localize('hunt-the-code', 'admin-command-description'),
defaultPermission: false,
options: [
{
type: 'SUB_COMMAND',
name: 'create-code',
description: localize('hunt-the-code', 'create-code-description'),
options: [
{
type: 'STRING',
name: 'display-name',
required: true,
description: localize('hunt-the-code', 'display-name-description')
},
{
type: 'STRING',
name: 'code',
required: false,
description: localize('hunt-the-code', 'code-description')
}
]
},
{
type: 'SUB_COMMAND',
name: 'end',
description: localize('hunt-the-code', 'end-description')
},
{
type: 'SUB_COMMAND',
name: 'report',
description: localize('hunt-the-code', 'report-description')
}
]
};
114 changes: 114 additions & 0 deletions modules/hunt-the-code/commands/hunt-the-code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
const {localize} = require('../../../src/functions/localize');
const {embedType} = require('../../../src/functions/helpers');
const {MessageEmbed} = require('discord.js');

module.exports.subcommands = {
'redeem': async function (interaction) {
const moduleStrings = interaction.client.configurations['hunt-the-code']['strings'];
const codeObject = await interaction.client.models['hunt-the-code']['Code'].findOne({
where: {
code: interaction.options.getString('code').toUpperCase()
}
});
if (!codeObject) return interaction.reply(embedType(moduleStrings.codeNotFoundMessage, {}, {ephemeral: true}));
const [user] = await interaction.client.models['hunt-the-code']['User'].findOrCreate({
where: {
id: interaction.user.id
}
});
if (user.foundCodes.includes(codeObject.code)) return interaction.reply(embedType(moduleStrings.codeAlreadyRedeemed, {
'%userCodesCount%': user.foundCount,
'%displayName%': codeObject.displayName,
'%codeUseCount%': codeObject.foundCount
}, {ephemeral: true}));
user.foundCount++;
user.foundCodes = [...user.foundCodes, codeObject.code];
await user.save();
codeObject.foundCount++;
interaction.reply(embedType(moduleStrings.codeRedeemed, {
'%displayName%': codeObject.displayName,
'%codeUseCount%': codeObject.foundCount,
'%userCodesCount%': user.foundCount
}, {ephemeral: true}));
await codeObject.save();
},
'profile': async function (interaction) {
const [user] = await interaction.client.models['hunt-the-code']['User'].findOrCreate({
where: {
id: interaction.user.id
}
});
const codes = await interaction.client.models['hunt-the-code']['Code'].findAll({
attributes: ['displayName', 'code']
});
let foundCodes = '';
for (const code of user.foundCodes) {
const codeObject = codes.find(c => c.code === code);
if (!codeObject) continue;
foundCodes = foundCodes + `\n• ${codeObject.displayName}`;
}
if (!foundCodes) foundCodes = localize('hunt-the-code', 'no-codes-found');
interaction.reply(embedType(interaction.client.configurations['hunt-the-code']['strings'].profileMessage, {
'%username%': interaction.user.username,
'%foundCount%': user.foundCount,
'%allCodesCount%': codes.length,
'%foundCodes%': foundCodes
}, {ephemeral: true}));
},
'leaderboard': async function (interaction) {
const moduleStrings = interaction.client.configurations['hunt-the-code']['strings'];
const users = await interaction.client.models['hunt-the-code']['User'].findAll({
attributes: ['id', 'foundCount'],
order: [
['foundCount', 'DESC']
],
limit: 20
});
let userString = '';
for (const user of users) {
userString = userString + `\n<@${user.id}>: ${user.foundCount}`;
}
if (userString === '') userString = localize('hunt-the-code', 'no-users');
const embed = new MessageEmbed()
.setDescription(userString)
.setTitle(moduleStrings.leaderboardMessage.title)
.setImage(moduleStrings.leaderboardMessage.image || null)
.setThumbnail(moduleStrings.leaderboardMessage.thumbnail || null)
.setColor(moduleStrings.leaderboardMessage.color);
interaction.reply({
ephemeral: true,
embeds: [embed]
});
}
};

module.exports.config = {
name: 'hunt-the-code',
description: localize('hunt-the-code', 'command-description'),
defaultPermission: true,
options: [
{
type: 'SUB_COMMAND',
name: 'redeem',
description: localize('hunt-the-code', 'redeem-description'),
options: [
{
type: 'STRING',
name: 'code',
required: true,
description: localize('hunt-the-code', 'code-redeem-description')
}
]
},
{
type: 'SUB_COMMAND',
name: 'leaderboard',
description: localize('hunt-the-code', 'leaderboard-description')
},
{
type: 'SUB_COMMAND',
name: 'profile',
description: localize('hunt-the-code', 'profile-description')
}
]
};
27 changes: 27 additions & 0 deletions modules/hunt-the-code/models/Code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const {DataTypes, Model} = require('sequelize');

module.exports = class HuntTheCodeCode extends Model {
static init(sequelize) {
return super.init({
code: {
type: DataTypes.STRING,
primaryKey: true
},
creatorID: DataTypes.STRING,
displayName: DataTypes.STRING,
foundCount: {
type: DataTypes.INTEGER,
defaultValue: 0
}
}, {
tableName: 'hunt-the-code_Code',
timestamps: true,
sequelize
});
}
};

module.exports.config = {
'name': 'Code',
'module': 'hunt-the-code'
};
29 changes: 29 additions & 0 deletions modules/hunt-the-code/models/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const {DataTypes, Model} = require('sequelize');

module.exports = class HuntTheCodeUser extends Model {
static init(sequelize) {
return super.init({
id: {
type: DataTypes.STRING,
primaryKey: true
},
foundCount: {
type: DataTypes.INTEGER,
defaultValue: 0
},
foundCodes: {
type: DataTypes.JSON,
defaultValue: []
}
}, {
tableName: 'hunt-the-code_User',
timestamps: true,
sequelize
});
}
};

module.exports.config = {
'name': 'User',
'module': 'hunt-the-code'
};
20 changes: 20 additions & 0 deletions modules/hunt-the-code/module.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "hunt-the-code",
"humanReadableName-en": "Hunt the code",
"humanReadableName-de": "Sammel die Codes",
"author": {
"scnxOrgID": "1",
"name": "SCDerox (SC Network Team)",
"link": "https://github.com/SCDerox"
},
"openSourceURL": "https://github.com/SCNetwork/CustomDCBot/tree/main/modules/hunt-the-code",
"description-en": "Hide codes and let your users collect them",
"description-de": "Verstecke Codes und lasse sie von deinen Nutzern sammeln",
"commands-dir": "/commands",
"models-dir": "/models",
"fa-icon": "fa-solid fa-tags",
"config-example-files": [
"strings.json"
],
"tags": ["community"]
}
Loading