Skip to content
This repository has been archived by the owner on Jul 7, 2024. It is now read-only.

Commit

Permalink
Ducks: Added ducks to the default config, added a timeout Map
Browse files Browse the repository at this point in the history
  • Loading branch information
Cpt-Dingus committed Aug 28, 2023
1 parent f5d37a5 commit f84e597
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 36 deletions.
17 changes: 17 additions & 0 deletions config.default.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,23 @@
"maxLength": 100,
"pasteFooterContent": "",
"pasteApi": ""
},
"duck": {
"enabled": true,
"channels": [],
// Times are specified in seconds
"minimumSpawn": 30,
"maximumSpawn": 180,
"runAwayTime": 20,
// Timeout on miss
"cooldown": 5,
// Failure percentages
"failRates": {
// Bef and bang
"interaction": 50,
"kill": 50,
"donate": 50
}
}
}
}
70 changes: 34 additions & 36 deletions src/modules/duck/duck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ interface duckUser {
speedRecord: number;
}

/** Interface used for miss checks, where:
* User id: Unix timestamp of timeout end
*/
interface missMap {
[user: Snowflake]: number;
}

/** The root 'duck' module definition */
const duck = new util.RootModule('duck', 'Duck management commands', [
util.mongo,
Expand Down Expand Up @@ -227,39 +234,8 @@ async function handleBefriend(user: User, speed: number, channel: TextChannel) {
{name: 'Friends', value: updatedCount?.toString() ?? '1', inline: true},
{name: 'Kills', value: locatedUser?.killed.toString() ?? '0', inline: true},
]);
duck.registerSubModule(
new util.SubModule(
'record',
'Gets the current global speed record',
[],
async () => {
const speedRecord: duckUser | null = await getGlobalSpeedRecord();

if (!speedRecord) {
return util.embed.errorEmbed('Noone has set a global record yet!');
}

const embed: EmbedBuilder = new EmbedBuilder()
.setColor(Colors.Green)
.setThumbnail(DUCK_PIC_URL)
.setTitle('Duck speed record')
.setFields([
{
name: 'Time',
value: `${speedRecord.speedRecord.toString()} seconds`,
inline: true,
},
{
name: 'Record holder',
value: `<@!${speedRecord.user}>`,
inline: true,
},
]);

await channel.send({embeds: [embed]});
}
)
);
await channel.send({embeds: [embed.toJSON()]});
}

/** Function to add a killed duck to the DB
Expand Down Expand Up @@ -335,7 +311,7 @@ async function miss(
member: GuildMember,
channel: TextChannel
): Promise<boolean> {
if (Math.random() <= FAIL_RATES.interaction! / 100) {
if (Math.random() >= FAIL_RATES.interaction! / 100) {
return false;
}
const embed: EmbedBuilder = new EmbedBuilder()
Expand All @@ -349,10 +325,8 @@ async function miss(
await member.timeout(COOLDOWN! * 1000, 'Missed a duck');
}

const timeoutMessage = await channel.send({embeds: [embed]});
await channel.send({embeds: [embed]});

// Deleted the timeout message after 5 seconds
setTimeout(async () => await timeoutMessage.delete(), COOLDOWN! * 1_000!);
return true;
}

Expand All @@ -363,15 +337,38 @@ async function summonDuck(channel: TextChannel): Promise<void> {
time: duck.config.runAwayTime * 1_000,
filter: message => ['bef', 'bang'].includes(message.content),
});
const misses: missMap = {};
let caught = false;

duckCollector.on('collect', async message => {
const time =
(message.createdTimestamp - duckMessage.createdTimestamp) / 1000;

// The person missed within <COOLDOWN> seconds ago
if (
misses[message.author.id] !== undefined &&
Date.now() <= misses[message.author.id]
) {
// All errors are catched since the only possible one is that the author has disabled dms
await message.author
.send(`I said to wait for ${COOLDOWN!} seconds! Resetting the timer...`)
.catch();
// This informs the author that they got timed out regardless of their dm status
await message.react('🕗');
// Resets the timer
misses[message.author.id] = Date.now() + COOLDOWN! * 1_000;
return;
}

// The timeout has passed, just remove the value from the miss cache
if (misses[message.author.id] !== undefined) {
delete misses[message.author.id];
}

switch (message.content) {
case 'bef':
if (await miss(message.member!, channel)) {
misses[message.author.id] = Date.now() + COOLDOWN! * 1_000;
break;
} else {
await duckMessage.delete();
Expand All @@ -384,6 +381,7 @@ async function summonDuck(channel: TextChannel): Promise<void> {

case 'bang':
if (await miss(message.member!, channel)) {
misses[message.author.id] = Date.now() + COOLDOWN! * 1_000;
break;
} else {
await duckMessage.delete();
Expand Down

0 comments on commit f84e597

Please sign in to comment.