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

Commit

Permalink
Merge pull request #28 from EliasSchaut/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
EliasSchaut authored Jan 15, 2022
2 parents f9eec21 + 0acc6b1 commit c40706e
Show file tree
Hide file tree
Showing 50 changed files with 5,777 additions and 48 deletions.
5,557 changes: 5,557 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

23 changes: 10 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
{
"name": "@elias.schaut/discord-bot-template",
"version": "1.0.0-beta.5",
"version": "1.0.0-beta.6",
"description": "A template for creating discord bots with node.js and discord.js! (See also https://github.com/EliasSchaut/Discord-Bot-Template)",
"main": "index.js",
"devDependencies": {
"@types/jest": "^23.3.12",
"jest": "^27.0.6"
},
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node .",
"pm": "pm2 start index.js --name my-bot",
"pm": "pm2 start src/index.js --name my-bot",
"pm-restart": "pm2 reload my-bot",
"pm-stop": "pm2 delete my-bot"
},
"dependencies": {
"dayjs": "^1.10.7",
"@discordjs/builders": "^0.11.0",
"@discordjs/rest": "^0.2.0-canary.0",
"discord-api-types": "^0.22.0",
"discord.js": "^13.5.0",
"dayjs": "^1.10.7",
"discord-api-types": "^0.26.1",
"discord.js": "^13.6.0",
"ms": "^2.1.3",
"node": "^16.13.1",
"node": "^16.13.2",
"node-schedule": "^2.1.0",
"pm2": "^5.1.2",
"sequelize": "^6.12.0",
"sequelize": "^6.13.0",
"sqlite3": "^4.2.0",
"winston": "^3.3.3"
"winston": "^3.4.0"
},
"keywords": [
"discord-bot-template"
Expand Down
2 changes: 1 addition & 1 deletion commands/help/help.js → src/commands/help/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

const Discord = require("discord.js")
const { MessageActionRow, MessageSelectMenu } = require('discord.js');
const { get_text: gt } = require("../../lang/lang_helper")
const { get_text: gt } = require("../../lang/lang_man")
const s = "commands.help."

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion commands/help/reload.js → src/commands/help/reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// ===============================

const fs = require('fs');
const { get_text: gt } = require("../../lang/lang_helper")
const { get_text: gt } = require("../../lang/lang_man")
const s = "commands.reload."
const commands_path = "./commands"

Expand Down
4 changes: 2 additions & 2 deletions commands/settings/lang.js → src/commands/settings/lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
// If lang is called with a valid language as argument (see config), it will change the language for the author from message to it
// ===============================

const { get_text: gt, is_valid: iv } = require("../../lang/lang_helper")
const { enable_lang_change, lang_paths } = require("../../config/config.json")
const { get_text: gt, is_valid: iv } = require("../../lang/lang_man")
const { enable_lang_change, lang_paths } = require("../../../config/config.json")
const s = "commands.lang."

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
// If prefix is called with a prefix as argument, it will change the prefix for the guild from message to it.
// ===============================

const { get_text: gt } = require("../../lang/lang_helper")
const { enable_prefix_change } = require("../../config/config.json")
const { get_text: gt } = require("../../lang/lang_man")
const { enable_prefix_change } = require("../../../config/config.json")
const s = "commands.prefix."

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion commands/test/echo.js → src/commands/test/echo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This is useful to check, if the bot is still alive.
// ===============================

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

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion commands/test/ping.js → src/commands/test/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The ping command returns the websocket heartbeat and the roundtrip latency.
// ===============================

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

module.exports = {
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions db/models/Guild.js → src/db/models/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const _TABLE = (sequelize, Sequelize) => {
type: Sequelize.STRING,
unique: true,
},
prefix: Sequelize.TEXT,
prefix: Sequelize.TEXT
}, {
timestamp: false
})
Expand Down Expand Up @@ -37,7 +37,7 @@ async function add(client, guild_id) {
try {
await client.DB.Guild.TABLE.create({
guild_id: guild_id,
prefix: client.config.prefix,
prefix: client.config.prefix
})
client.logger.log("info",`guild ${guild_id} successfully added to database 'Guild'`)

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// ---------------------------------
async function interaction_create(interaction) {
// add more specific button handlers here and below in Specific Button Handlers


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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const schedule = require('node-schedule')
// Export
// ---------------------------------
async function init(client) {
init_mensa_event(client)

}

async function event_create(msg) {
Expand All @@ -18,17 +18,14 @@ async function event_create(msg) {
// ----------------------------------
// Inits
// ----------------------------------
function init_mensa_event(client) {
}

// ----------------------------------


// ----------------------------------
// Events
// ----------------------------------
function mensa_event(client) {

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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ async function interaction_create(interaction) {
// ---------------------------------
function interaction_to_message(interaction) {
interaction.author = interaction.user


return interaction
}
// ---------------------------------
Expand Down Expand Up @@ -61,4 +59,4 @@ function get_command(interaction, command_name) {

// ----------------------------------

module.exports = { interaction_create }
module.exports = { interaction_create, interaction_to_message }
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 = "./handler/cmd_modifications/mods"
const mods_path_global = "./src/handler/modification_handler/mods"
const mods_path = "./mods"
const mods = {}
// ----------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const config = require('../../../../Lila-Kuh-bot/config/config.json')
const config = require('../../../../config/config.json')
const choices = []
for (const lang of Object.keys(config.lang_paths)) {
choices.push({ "name": lang, "value": lang })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async function create_options(client, command) {
const args_min_length = await client.mods.args_min_length.get(null, command)
const args_max_length = await client.mods.args_max_length.get(null, command)

if (fs.existsSync(`./js/slash_commands/option_models/${name}.js`)) {
if (fs.existsSync(`./src/handler/slash_handler/option_models/${name}.js`)) {
const option_model = require(`./option_models/${name}.js`)
for (const option of option_model) {
options.push(create_option(option.name, option.description, option.required, option.choices))
Expand Down
18 changes: 8 additions & 10 deletions index.js → src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// ===============================
// This is the entry point of the whole program!
// This file will collect every needed package or code file together and start the discord bot.
// In this file is also the event listener for every incoming message for the bot.
// This file checks, if the message is a valid command and if so, it will execute.
// Further explanations can be found in the wiki: https://github.com/EliasSchaut/Discord-Bot-Template/wiki
// ===============================


Expand All @@ -22,31 +21,30 @@ const client = new Discord.Client({ intents: [

// get required methods and fields and save it into client. This will always be accessible with message.client
client.commands = new Discord.Collection()
client.config = require('./config/config.json')
client.config = require('../config/config.json')
client.helper = require('./util/cmd_helper')
client.lang_helper = require("./lang/lang_helper")
client.lang_helper = require("./lang/lang_man")
client.DB = require('./db/db_init').DB
client.sequelize = require('./db/db_init').sequelize
client.logger = require("./util/logger").logger
client.slasher = require("./handler/slash_commands/slasher")
client.slasher = require("./handler/slash_handler/slasher")
client.command_event = require("./handler/event_handler/command_event")
client.slash_event = require("./handler/event_handler/slash_event")
client.menu_event = require("./handler/event_handler/menu_event")
client.button_event = require("./handler/event_handler/button_event")
client.events = require("./handler/event_handler/events")
client.output = require("./util/output")
client.mod_man = require("./handler/cmd_modifications/mod_manager")
client.mod_man = require("./handler/modification_handler/mod_manager")

// dynamically retrieve all command files and additionally save it into message.client.command_tree
async function load_commands(client) {
let command_tree = {}
const commands_path = "./commands"
const commandFolders = fs.readdirSync(commands_path)
const commandFolders = fs.readdirSync("./src/commands")
for (const folder of commandFolders) {
command_tree[folder] = {}
const commandFiles = fs.readdirSync(`${commands_path}/${folder}`).filter(file => file.endsWith('.js'))
const commandFiles = fs.readdirSync(`./src/commands/${folder}`).filter(file => file.endsWith('.js'))
for (const file of commandFiles) {
const command = require(`${commands_path}/${folder}/${file}`)
const command = require(`./commands/${folder}/${file}`)
if (await client.mods.disabled.get(null, command)) continue

const name = await client.mods.name.get(null, command)
Expand Down
2 changes: 1 addition & 1 deletion lang/lang_helper.js → src/lang/lang_man.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// This file provides different useful methods about languages
// ===============================

const { lang_paths } = require("../config/config.json")
const { lang_paths } = require("../../config/config.json")

// get all language files and save it into text
const text = {}
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion util/cmd_helper.js → src/util/cmd_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Check Msg
// ----------------------------
const Discord = require("discord.js");
const {get_text: gt} = require("../lang/lang_helper");
const {get_text: gt} = require("../lang/lang_man");

// checks, if given message is from dm (personal chat with bot)
function from_dm(msg) {
Expand Down
2 changes: 1 addition & 1 deletion util/logger.js → src/util/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

// require winston.handler for logging
const winston = require("winston");
const config = require("../config/config.json");
const config = require("../../config/config.json");

// create logger; if enable_logging is true, logger will also log in file
const logger = (config.enable_logging) ?
Expand Down
2 changes: 1 addition & 1 deletion util/output.js → src/util/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async function edit(msg_to_edit, new_content) {
// Check Slash
// ----------------------------
function check_slash(msg) {
return (msg.type === 'APPLICATION_COMMAND') && (!Object.keys(msg).includes("interaction"))
return ((msg.type === 'APPLICATION_COMMAND') && (!Object.keys(msg).includes("interaction"))) || (msg.type === 'MESSAGE_COMPONENT')
}
// ----------------------------

Expand Down
24 changes: 24 additions & 0 deletions templates/command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// See also: https://github.com/EliasSchaut/Discord-Bot-Template/wiki/How-to-command

const { get_text: gt } = require("../../lang/lang_helper")
const s = "commands.<name>."

module.exports = {
name: '<name>',
description: async function (msg) { return await gt(msg, s + "help") },
aliases: ['<alias_1>', '<alias_2>'],
args_needed: false,
args_min_length: 0,
args_max_length: 0,
usage: async function (msg) { return await gt(msg, s + "usage") },
guild_only: false,
dm_only: false,
need_permission: ['<permission1>', '<permission2>'],
admin_only: false,
nsfw: false,
disabled: false,
enable_slash: false,
async execute(msg, args) { // msg = discord.js 'Message' object; args = given arguments as list
// your lovely code to execute
},
};
86 changes: 86 additions & 0 deletions templates/db_model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// See also: https://github.com/EliasSchaut/Discord-Bot-Template/wiki/How-to-database
// Sequelize-Types: https://sequelize.org/v5/manual/data-types.html
// Examples: https://discordjs.guide/sequelize/

// ---------------------------------------------
// Model
// ---------------------------------------------
const _TABLE = (sequelize, Sequelize) => {
return sequelize.define('<NAME>', {
"<attribute1>": {
type: "<Sequelize-Type>",
unique: true,
},
"<attribute2>": "<Sequelize-Type>",
}, {
timestamp: false
})
}
// ---------------------------------------------


// ---------------------------------------------
// Helper
// ---------------------------------------------
// add stuff to database
async function add(msg) {
try {
await msg.client.DB["<NAME>"].TABLE.create({
"<attribute1>": "<value1>",
"<attribute2>": "<value2>"
})

} catch (e) {
if (e.name === 'SequelizeUniqueConstraintError') {
msg.client.logger.log("warn",`<value> already exist in database!`)

} else {
msg.client.logger.log("error",`Something went wrong with adding this <values> in database!`)
}
}
}

// get stuff from database
async function get(msg) {
const tag = await msg.client.DB["<NAME>"].TABLE.findOne({ where: { "<attribute1>": "<value1>" } })

if (tag) {
return tag.lang

} else {
msg.client.logger.log("warn",`<value> not in database!`)
await add(msg)
return await get(msg)
}
}


// set stuff in database
async function set(msg) {
const new_tag = await msg.client.DB["<NAME>"].TABLE.update({ "<attribute2>": "<value3>" }, { where: { "<attribute1>": "<value1>" } })

if (new_tag) {
return true

} else {
msg.client.logger.log("error", `Could not set <attribute2>!`)
return false
}
}

// remove tag in database
async function remove(msg) {
const rowCount = await msg.client.DB["<NAME>"].TABLE.destroy({ where: { "<attribute1>": "<value1>" } })

if (rowCount) {
return true

} else {
msg.client.logger.log("error", `Could not delete tag with <attribute1>!`)
return false
}
}
// ---------------------------------------------


module.exports = { _TABLE, add, get, set, remove }
Loading

0 comments on commit c40706e

Please sign in to comment.