Skip to content

Commit

Permalink
Merge pull request #11 from Lila-Kuhlt/#2
Browse files Browse the repository at this point in the history
  • Loading branch information
EliasSchaut committed Jan 3, 2022
2 parents 1c0894a + 57e2a99 commit 9a2433c
Show file tree
Hide file tree
Showing 41 changed files with 316 additions and 69 deletions.
19 changes: 19 additions & 0 deletions commands/mensa/mensa_disable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { get_text: gt } = require("../../lang/lang_helper")
const s = "commands.mensa_disable."

module.exports = {
name: 'mensa_disable',
description: async function (msg) { return await gt(msg, s + "help") },
aliases: ['md', 'mensad', 'mensadisable', "mensa_d", "mensa_dis"],
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_mensa_disable(msg.client, msg.member.guild.id)
await msg.client.output.send(msg, await gt(msg, s + "success"))
},
};
24 changes: 24 additions & 0 deletions commands/mensa/mensa_enable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { get_text: gt } = require("../../lang/lang_helper")
const s = "commands.mensa_enable."

module.exports = {
name: 'mensa_enable',
description: async function (msg) { return await gt(msg, s + "help") },
aliases: ['me', 'mensae', 'mensaenable', "mensa_e"],
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_mensa_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_mensa_enable(msg.client, msg.member.guild.id)
await msg.client.output.send(msg, await gt(msg, s + "success"))
},
};
38 changes: 38 additions & 0 deletions commands/mensa/mensa_set_channel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const { get_text: gt } = require("../../lang/lang_helper")
const s = "commands.mensa_set_channel."

module.exports = {
name: 'mensa_set_channel',
description: async function (msg) { return await gt(msg, s + "help") },
aliases: ['ms', 'mset', 'msc', 'mensas', 'mensa_set', 'mensaset'],
args_needed: true,
args_min_length: 1,
args_max_length: 1,
usage: async function (msg) { return await gt(msg, s + "usage") },
guild_only: true,
need_permission: ['ADMINISTRATOR'],
disabled: false,
enable_slash: false,
async execute(msg, args) {
const new_mensa_channel_id = args[0]

// args is a number
if (new_mensa_channel_id.match(/[^0-9]/)) {
await msg.client.output.reply(msg, await gt(msg, s + "fail.no_number"))
return
}

// channel exists in same guild#
let channel
try {
channel = await msg.guild.channels.fetch(new_mensa_channel_id)

} catch (e) {
await msg.client.output.reply(msg, await gt(msg, s + "fail.wrong_id"))
return
}

await msg.client.DB.Guild.set_mensa_channel_id(msg.client, msg.member.guild.id, new_mensa_channel_id)
await msg.client.output.send(msg, await gt(msg, s + "success"))
},
};
4 changes: 1 addition & 3 deletions config/config-template.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,5 @@

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

"mensa_channel_id": "900087023338151946"
"auto_slash_options": false
}
119 changes: 108 additions & 11 deletions db/models/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ const _TABLE = (sequelize, Sequelize) => {
unique: true,
},
prefix: Sequelize.TEXT,
mensa_channel_id: {
type: Sequelize.STRING,
unique: true,
},
mensa_enabled: Sequelize.BOOLEAN,
bday_channel_id: {
type: Sequelize.STRING,
unique: true,
},
bday_enabled: Sequelize.BOOLEAN
}, {
timestamp: false
})
Expand All @@ -18,27 +28,52 @@ const _TABLE = (sequelize, Sequelize) => {
// ---------------------------------------------
// Helper
// ---------------------------------------------
// -------------
// General
// -------------
// get all guild_ids
async function get_guild_ids(client) {
const tag = await client.DB.Guild.TABLE.findAll({attributes: ["guild_id"]})
return (tag) ? tag.map(function (e) {
return e.dataValues.guild_id
}) : []
}


// add the guild from message in the database 'Guild'. Also set prefix to config.prefix
async function add(msg) {
msg.client.logger.log("info", `try to add guild ${msg.member.guild.name} to database 'Guild'`)
async function add(client, guild_id) {
client.logger.log("info", `try to add guild ${guild_id} to database 'Guild'`)

try {
await msg.client.DB.Guild.TABLE.create({
guild_id: msg.member.guild.id,
prefix: msg.client.config.prefix
await client.DB.Guild.TABLE.create({
guild_id: guild_id,
prefix: client.config.prefix,
mensa_channel_id: null,
mensa_enabled: false,
bday_channel_id: null,
bday_enabled: false
})
msg.client.logger.log("info",`guild ${msg.member.guild.name} successfully added to database 'Guild'`)
client.logger.log("info",`guild ${guild_id} successfully added to database 'Guild'`)

} catch (e) {
if (e.name === 'SequelizeUniqueConstraintError') {
msg.client.logger.log("warn",`guild ${msg.member.guild.name} already exist in database 'Guild'`)
client.logger.log("warn",`guild ${guild_id} already exist in database 'Guild'`)

} else {
msg.client.logger.log("error",`Something went wrong with adding guild ${msg.member.guild.name} in database 'Guild'`)
client.logger.log("error",`Something went wrong with adding guild ${guild_id} in database 'Guild'`)
}
}
}

async function remove(client, guild_id) {

}
// -------------


// -------------
// Prefix
// -------------
// get prefix of the guild from message. If guild doesn't exist in database, the guild will added into it
async function get_prefix(msg) {
if (msg.client.helper.from_dm(msg)) {
Expand All @@ -52,7 +87,7 @@ async function get_prefix(msg) {

} else {
msg.client.logger.log("warn",`guild ${msg.member.guild.name} not in database 'Guild'`)
await add(msg)
await add(msg.client, msg.member.guild.id)
return await get_prefix(msg)
}
}
Expand All @@ -70,11 +105,73 @@ async function set_prefix(msg, new_prefix) {
return false
}
}
// -------------


// -------------
// Mensa
// -------------
// get mensa_channel_id of the guild. If guild doesn't exist in database, the guild will add it into the database
async function get_mensa_channel_id(client, guild_id) {
const tag = await client.DB.Guild.TABLE.findOne({ where: { guild_id: guild_id } })

async function remove(msg, guild_id) {
if (tag) {
return tag.mensa_channel_id

} else {
client.logger.log("warn",`guild ${guild_id} not in database 'Guild'`)
await add(client, guild_id)
return await get_mensa_channel_id(client, guild_id)
}
}

// set mensa_channel_id
async function set_mensa_channel_id(client, guild_id, new_channel_id) {
const old_channel_id = await get_mensa_channel_id(client, guild_id)
const new_tag = await client.DB.Guild.TABLE.update({ mensa_channel_id: new_channel_id }, { where: { guild_id: guild_id } })

if (new_tag) {
return true

} else {
client.logger.log("error", `Could not set mensa_channel_id from ${old_channel_id} to ${new_channel_id} of guild ${guild_id} in database 'Guild'`)
return false
}
}

async function set_mensa_enable(client, guild_id) {
const new_tag = await client.DB.Guild.TABLE.update({ mensa_enabled: true }, { where: { guild_id: guild_id } })

if (new_tag) {
return true

} else {
client.logger.log("warn",`guild ${guild_id} not in database 'Guild'`)
await add(client, guild_id)
return await set_mensa_enable(client)
}
}

async function set_mensa_disable(client, guild_id) {
const new_tag = await client.DB.Guild.TABLE.update({ mensa_enabled: false }, { where: { guild_id: guild_id } })

if (new_tag) {
return true

} else {
client.logger.log("warn",`guild ${guild_id} not in database 'Guild'`)
await add(client, guild_id)
return await set_mensa_disable(client, guild_id)
}
}

async function get_mensa_enabled(client, guild_id) {
const tag = await client.DB.Guild.TABLE.findOne({ where: { guild_id: guild_id } })
return (tag) ? tag.mensa_enabled : false
}
// -------------
// ---------------------------------------------


module.exports = { _TABLE, add, get_prefix, set_prefix, remove }
module.exports = { _TABLE, add, get_guild_ids, get_prefix, set_prefix,
get_mensa_channel_id, set_mensa_channel_id, set_mensa_disable, set_mensa_enable, get_mensa_enabled }
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Global values
// ----------------------------------
const fs = require("fs")
const mods_path_global = "./js/cmd_modifications/mods"
const mods_path_global = "./handler/cmd_modifications/mods"
const mods_path = "./mods"
const mods = {}
// ----------------------------------
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// event_helper fields
// event_handler fields
const s = "command_create."

// ---------------------------------
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { MessageEmbed } = require('discord.js')
const mensa_channel_id = require('../../../config/config.json').mensa_channel_id

const dayjs = require('dayjs')
const weekOfYear = require('dayjs/plugin/weekOfYear')
Expand All @@ -19,13 +18,17 @@ const sw_link = {
// Export
// ---------------------------------
async function post_mensa_plan(client) {
const data = await get_meal_json(client, get_meal_api_link())
const guild_ids = await client.DB.Guild.get_guild_ids(client)
if (!guild_ids.length) return

if (data === null) send_fail(client)
const data = await get_meal_json(client, get_meal_api_link())
let embed
if (data === null) embed = generate_embed_fail(client)
else {
const fields = formatted_meals_to_fields(format_meals(data))
send_success(client, fields)
embed = generate_embed_success(client, fields)
}
await send_all_success(client, guild_ids, embed)
}
// ---------------------------------

Expand All @@ -34,29 +37,42 @@ async function post_mensa_plan(client) {
// ---------------------------------
// Send
// ---------------------------------
async function send_fail(client) {
const channel = client.channels.cache.get(mensa_channel_id)
const embed = new MessageEmbed()
async function send_all_success(client, guild_ids, embed) {
for (const guild_id of guild_ids) {
const enabled = await client.DB.Guild.get_mensa_enabled(client, guild_id)
if (!enabled) continue

const mensa_channel_id = await client.DB.Guild.get_mensa_channel_id(client, guild_id)
if (!mensa_channel_id) continue

try {
const mensa_channel = await client.channels.fetch(mensa_channel_id)
mensa_channel.send({ embeds: [embed] })
} catch (e) {}
}
}
// ---------------------------------

// ---------------------------------
// Embeds
// ---------------------------------
function generate_embed_fail(client) {
return new MessageEmbed()
.setColor(client.config.embed.color)
.setAuthor("Lila Pause", client.config.embed.avatar_url)
.setAuthor({ name: "Lila Pause", iconURL: client.config.embed.avatar_url })
.setTitle("Mensa Update Error")
.setDescription("Keine Mensa Daten für heute :(")
.setFooter("Update immer So-Do um 15:00 Uhr für den Folgetag!")

channel.send({ embeds: [embed] })
.setFooter({ text: "Update immer So-Do um 15:00 Uhr für den Folgetag!" })
}

async function send_success(client, fields) {
const channel = client.channels.cache.get(mensa_channel_id)
const embed = new MessageEmbed()
function generate_embed_success(client, fields) {
return new MessageEmbed()
.setColor(client.config.embed.color)
.setAuthor(client.config.embed.author_name, client.config.embed.avatar_url)
.setAuthor({ name: client.config.embed.author_name, iconURL: client.config.embed.avatar_url })
.setTitle("Mensa Update für " + get_date())
.addFields(fields)
.setURL(get_sw_link())
.setFooter("Update immer So-Do um 15:00 Uhr für den Folgetag!")

channel.send({ embeds: [embed] })
.setFooter({ text: "Update immer So-Do um 15:00 Uhr für den Folgetag!" })
}
// ---------------------------------

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 9a2433c

Please sign in to comment.