Skip to content

Commit

Permalink
Merge pull request #22 from Lila-Kuhlt/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
EliasSchaut authored Feb 10, 2022
2 parents fe53c2f + 4cf8c56 commit f24a738
Show file tree
Hide file tree
Showing 29 changed files with 1,130 additions and 28 deletions.
9 changes: 7 additions & 2 deletions config/config-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},

"help": {
"show_only_permitted_commands": false,
"show_only_permitted_commands": true,
"send_to_dm": false,
"show_cmd_modifications": true
},
Expand All @@ -37,5 +37,10 @@

"enable_standard_commands": true,
"enable_slash_commands": true,
"auto_slash_options": false
"auto_slash_options": true,

"date": {
"localisation": "de",
"format": "DD.MM.YYYY"
}
}
21 changes: 19 additions & 2 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lila.kuhlt/lila-kuh-bot",
"version": "1.0.0-beta.2",
"version": "1.0.0-beta.3",
"description": "Official bot of the Lila Pause",
"main": "src/index.js",
"scripts": {
Expand All @@ -18,6 +18,7 @@
"discord-api-types": "^0.26.1",
"discord.js": "^13.6.0",
"follow-redirects": ">=1.14.7",
"horoscope": "2.0.1",
"ms": "^2.1.3",
"node-schedule": "^2.1.0",
"pm2": "^5.1.2",
Expand Down
100 changes: 100 additions & 0 deletions src/commands/bday/bday.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// See also: https://github.com/EliasSchaut/Discord-Bot-Template/wiki/How-to-command

const { get_text: gt } = require("../../lang/lang_man")
const s = "commands.bday."
const { MessageEmbed } = require("discord.js")
const dayjs = require("dayjs")
const isSameOrBefore = require('dayjs/plugin/isSameOrBefore')
dayjs.extend(isSameOrBefore)
const objectSupport = require("dayjs/plugin/objectSupport")
dayjs.extend(objectSupport)
const relativeTime = require('dayjs/plugin/relativeTime')
dayjs.extend(relativeTime)
const horoscope = require("horoscope")

module.exports = {
name: 'bday',
description: async function (msg) { return await gt(msg, s + "help") },
aliases: ['bd'],
args_needed: false,
args_min_length: 0,
args_max_length: 1,
usage: async function (msg) { return await gt(msg, s + "usage") },
guild_only: true,
disabled: false,
enable_slash: true,
async execute(msg, args) {
// ----------------------------
// Checker
// ----------------------------
let member_id
if (args.length === 0) {
member_id = msg.author.id

} else if (/^<@!?\d+>$/.test(args[0])) {
member_id = args[0].match(/\d+/)[0]

} else if (/\d+/.test(args[0])) {
member_id = args[0]

} else {
return await msg.client.output.reply(msg, await gt(msg, `${s}fail.wrong_format`))
}

try {
await msg.guild.members.fetch(member_id)
} catch (e) {
return await msg.client.output.reply(msg, await gt(msg, `${s}fail.unknown_user`))
}
const bday_tag = await msg.client.DB.Bday.get(msg.client, msg.guildId, member_id)
if (bday_tag === null) return await msg.client.output.reply(msg, await gt(msg, `${s}fail.user_opt_out`))
// ----------------------------

// success
const embed = await this.post_embed_success(msg, member_id, bday_tag.year, bday_tag.month, bday_tag.day)
await msg.client.output.send(msg, { embeds: [embed] })
},
async post_embed_success(msg, bday_member_id, year, month, day) {
return new MessageEmbed()
.setColor(msg.client.config.embed.color)
.setAuthor({ name: msg.client.config.embed.author_name, iconURL: msg.client.config.embed.avatar_url })
.setTitle(await gt(msg, `${s}embed.title`))
.addFields(
await this.generate_general_field(msg, bday_member_id, year, month, day),
await this.generate_star_sign_field(msg, month, day),
await this.generate_time_calcs_field(msg, bday_member_id, year, month, day),
)
},
async generate_general_field(msg, bday_member_id, year, month, day) {
const date = dayjs(new Date(year, month, day))
const age = dayjs().year() - year - dayjs().isSameOrBefore({ month :month, day :day })
const format = msg.client.config.date.format
return {
name: await gt(msg, `${s}embed.fields.general`),
value: await gt(msg, `${s}success`, bday_member_id, date.format(format), age)
}
},
async generate_star_sign_field(msg, month, day) {
const sign = horoscope.getSign({ month: (month + 1), day: day })
const output_sign = await gt(msg, `${s}zodiac.signs.${sign}`)
const description = await gt(msg, `${s}zodiac.sign_descriptions.${sign}`)
return {
name: await gt(msg, `${s}embed.fields.zodiac`, output_sign),
value: description
}
},
async generate_time_calcs_field(msg, bday_member_id, year, month, day) {
const date = dayjs({ year: year, month: month, day: day })
const now = dayjs()
const diff = dayjs.duration(now.diff(date))
const days = await gt(msg, `${s}embed.fields.time_calcs.days`, parseInt(diff.asDays()))
const h = await gt(msg, `${s}embed.fields.time_calcs.h`, parseInt(diff.asHours()))
const ms = await gt(msg, `${s}embed.fields.time_calcs.ms`, diff.asMilliseconds())
const join = await gt(msg, `${s}embed.fields.time_calcs.or`)

return {
name: await gt(msg, `${s}embed.fields.time_calcs.title`),
value: await gt(msg, `${s}embed.fields.time_calcs.value`, bday_member_id, [days, h, ms].join(join))
}
}
};
53 changes: 53 additions & 0 deletions src/commands/bday/bday_all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// See also: https://github.com/EliasSchaut/Discord-Bot-Template/wiki/How-to-command

const { get_text: gt } = require("../../lang/lang_man")
const dayjs = require("dayjs")
const customParseFormat = require('dayjs/plugin/customParseFormat')
dayjs.extend(customParseFormat)
const s = "commands.bday_all."
const { MessageEmbed } = require("discord.js")

module.exports = {
name: 'bday_all',
description: async function (msg) { return await gt(msg, s + "help") },
aliases: ['bdaya', 'bda'],
args_needed: false,
args_min_length: 0,
args_max_length: 0,
guild_only: true,
disabled: false,
enable_slash: true,
async execute(msg, args) {
const user_ids = await msg.client.DB.Bday.get_user_ids(msg.client, msg.guildId)
const months = new Array(12).fill(0).map(() => { return [] })

for (const user_id of user_ids) {
try {
await msg.guild.members.fetch(user_id)
} catch (e) {
continue
}
const tag = await msg.client.DB.Bday.get(msg.client, msg.guildId, user_id)
if (tag === null) continue

const bdate = dayjs(new Date(tag.year, tag.month, tag.day)).format(msg.client.config.date.format)
months[tag.month].push(await gt(msg, `${s}embed.bday_entry`, bdate, user_id))
}

const embed = new MessageEmbed()
.setAuthor({ name: msg.client.config.embed.author_name, iconURL: msg.client.config.embed.avatar_url })
.setColor(msg.client.config.embed.color)
.setTitle(await gt(msg, `${s}embed.title`))


for (let i = 0; i < months.length; i++) {
if (months[i].length === 0) continue

const month_name = await gt(msg, `${s}months.${i}`)
months[i].sort()
embed.addField(month_name, months[i].join("\n"), true)
}

msg.client.output.send(msg, { embeds: [embed] })
},
};
21 changes: 21 additions & 0 deletions src/commands/bday/bday_disable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// See also: https://github.com/EliasSchaut/Discord-Bot-Template/wiki/How-to-command

const { get_text: gt } = require("../../lang/lang_man")
const s = "commands.bday_disable."

module.exports = {
name: 'bday_disable',
description: async function (msg) { return await gt(msg, s + "help") },
aliases: ['bdayd', 'bdd'],
args_needed: false,
args_min_length: 0,
args_max_length: 0,
guild_only: true,
need_permission: ['ADMINISTRATOR'],
disabled: false,
enable_slash: false,
async execute(msg, args) {
await msg.client.DB.Guild.set_bday_disable(msg.client, msg.member.guild.id)
await msg.client.output.send(msg, await gt(msg, s + "success"))
},
};
26 changes: 26 additions & 0 deletions src/commands/bday/bday_enable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// See also: https://github.com/EliasSchaut/Discord-Bot-Template/wiki/How-to-command

const { get_text: gt } = require("../../lang/lang_man")
const s = "commands.bday_enable."

module.exports = {
name: 'bday_enable',
description: async function (msg) { return await gt(msg, s + "help") },
aliases: ['bdaye', 'bde'],
args_needed: false,
args_min_length: 0,
args_max_length: 0,
guild_only: true,
need_permission: ['ADMINISTRATOR'],
disabled: false,
enable_slash: false,
async execute(msg, args) {
if (!await msg.client.DB.Guild.get_bday_channel_id(msg.client, msg.member.guild.id)) {
await msg.client.output.reply(msg, await gt(msg, s + "fail.channel_not_set"))
return
}

await msg.client.DB.Guild.set_bday_enable(msg.client, msg.member.guild.id)
await msg.client.output.send(msg, await gt(msg, s + "success"))
},
};
70 changes: 70 additions & 0 deletions src/commands/bday/bday_next.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// See also: https://github.com/EliasSchaut/Discord-Bot-Template/wiki/How-to-command

const { get_text: gt } = require("../../lang/lang_man")
const dayjs = require("dayjs")
const duration = require('dayjs/plugin/duration')
dayjs.extend(duration)
const isSameOrBefore = require('dayjs/plugin/isSameOrBefore')
dayjs.extend(isSameOrBefore)
const objectSupport = require("dayjs/plugin/objectSupport")
dayjs.extend(objectSupport)
const { MessageEmbed } = require("discord.js")
const s = "commands.bday_next."

module.exports = {
name: 'bday_next',
description: async function (msg) { return await gt(msg, s + "help") },
aliases: ['bdayn', 'bdn'],
args_needed: false,
args_min_length: 0,
args_max_length: 0,
guild_only: true,
disabled: false,
enable_slash: true,
async execute(msg, args) {
const user_ids = await msg.client.DB.Bday.get_user_ids(msg.client, msg.guildId)
const min = { day_distance: 400, user_ids: [-1] } // M = 400
const now = dayjs()

for (const user_id of user_ids) {
try {
await msg.guild.members.fetch(user_id)
} catch (e) {
continue
}
const tag = await msg.client.DB.Bday.get(msg.client, msg.guildId, user_id)
if (tag === null) continue

const bdate = dayjs({ day: tag.day, month: tag.month })
const day_distance = dayjs.duration(bdate.diff(dayjs(now))).asDays()
if (day_distance < 0) continue

const day_distance_int = parseInt(day_distance)
if (day_distance_int < min.day_distance) {
min.day_distance = day_distance_int
min.user_ids = [user_id]

} else if (day_distance_int === min.day_distance) {
min.user_ids.push(user_id)
}
}

if (min.user_ids === [-1]) return await msg.client.output.send(msg, await gt(msg, `${s}fail.nothing_found`))
for (const user_id of min.user_ids) {
const format = msg.client.config.date.format
const tag = await msg.client.DB.Bday.get(msg.client, msg.guildId, user_id)
const bdate = dayjs({ day: tag.day, month: tag.month, year: tag.year })
const new_age = dayjs().year() - tag.year - dayjs().isSameOrBefore({ month: tag.month, day: tag.day }) + 1

const embed = new MessageEmbed()
.setColor(msg.client.config.embed.color)

if (min.day_distance === 0) {
embed.setDescription(await gt(msg, `${s}embed.description_sg`, user_id, new_age, bdate.format(format)))
} else {
embed.setDescription(await gt(msg, `${s}embed.description_pl`, user_id, min.day_distance + 1, new_age, bdate.format(format)))
}
await msg.client.output.send(msg, { embeds: [embed] })
}
},
};
Loading

0 comments on commit f24a738

Please sign in to comment.