Skip to content

Commit

Permalink
Merge pull request #861 from Glazelf/modular
Browse files Browse the repository at this point in the history
Modular Ninigi
  • Loading branch information
Glazelf authored Jun 5, 2024
2 parents ad4e170 + 2ad2d88 commit e338e9c
Show file tree
Hide file tree
Showing 158 changed files with 2,289 additions and 2,104 deletions.
28 changes: 14 additions & 14 deletions affairs/birthday.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
module.exports = async (client) => {
const logger = require('../util/logger');
// Import globals
import Discord from "discord.js";
import logger from '../util/logger.js';
import globalVars from "../objects/globalVars.json" with { type: "json" };
import getRandomGif from "../util/getRandomGif.js";
import cron from "cron";
import { getBirthday } from "../database/dbServices/user.api.js";

export default async (client) => {
try {
const getRandomGif = require("../util/getRandomGif");
const cron = require("cron");
const timezone = 'utc';
const time = '05 00 06 * * *'; // Sec Min Hour, 8am CEST
const guildID = client.globalVars.ShinxServerID;
const channelID = client.globalVars.eventChannelID;
const Discord = require("discord.js");
const api_user = require('../database/dbServices/user.api');
const guildID = globalVars.ShinxServerID;
const channelID = globalVars.eventChannelID;
const gifTags = ["birthday"];
if (client.user.id != client.globalVars.NinigiID) return;
if (client.user.id != globalVars.NinigiID) return;
// Create cron job
new cron.CronJob(time, async () => {
let guild = await client.guilds.fetch(guildID);
Expand All @@ -25,9 +26,9 @@ module.exports = async (client) => {
let cutiesUsernames = [];
await guild.members.fetch();
// For every member check
for (m in [...guild.members.cache.values()]) {
for (const m in [...guild.members.cache.values()]) {
const member = [...guild.members.cache.values()][m];
const birthday = await api_user.getBirthday(member.id);
const birthday = await getBirthday(member.id);
if (birthday) {
let now = new Date();
// Birthdays are stored as string DDMM instead of being seperated by a -
Expand All @@ -44,14 +45,13 @@ module.exports = async (client) => {
const randomGif = await getRandomGif(gifTags);
// Create embed
const gifEmbed = new Discord.EmbedBuilder()
.setColor(client.globalVars.embedColor)
.setColor(globalVars.embedColor)
.setDescription(`Today is ${cutiesUsernames.join(' and ')}'s birthday, everyone!`)
.setImage(randomGif);
channel.send({ embeds: [gifEmbed], content: cuties.join(' ') });
}, timeZone = timezone, start = true);

} catch (e) {
// Log error
logger(e, client);
};
};
27 changes: 14 additions & 13 deletions affairs/stan.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
module.exports = async (client) => {
const logger = require('../util/logger');
import Discord from "discord.js";
import logger from '../util/logger.js';
import globalVars from "../objects/globalVars.json" with { type: "json" };
import getRandomGif from "../util/getRandomGif.js";
import cron from "cron";
import { incrementStanAmount, checkEvents } from "../database/dbServices/history.api.js";

export default async (client) => {
try {
const Discord = require("discord.js");
const api_history = require('../database/dbServices/history.api');
const getRandomGif = require("../util/getRandomGif");
const cron = require("cron");
const timezone = 'utc';
const time = '00 00 18 * * *'; // Sec Min Hour
const gifTags = ['pokemon', 'geass', 'dragon', 'game'];
const guildID = client.globalVars.ShinxServerID;
const guildID = globalVars.ShinxServerID;

if (client.user.id != client.globalVars.NinigiID) return;
if (client.user.id != globalVars.NinigiID) return;
// Create cronjob
new cron.CronJob(time, async () => {
let guild = await client.guilds.fetch(guildID);
Expand All @@ -23,16 +25,16 @@ module.exports = async (client) => {
let randomPick = Math.floor((Math.random() * (candidates.length - 0.1)));
let candidateRandom = candidates[randomPick];

await api_history.incrementStanAmount(candidateRandom.id);
await api_history.checkEvents();
await incrementStanAmount(candidateRandom.id);
await checkEvents();
// Random gif
const randomGif = await getRandomGif(gifTags);
if (!randomGif) return;

let channel = guild.channels.cache.find(channel => channel.id === client.globalVars.eventChannelID);
let channel = guild.channels.cache.find(channel => channel.id === globalVars.eventChannelID);

const gifEmbed = new Discord.EmbedBuilder()
.setColor(client.globalVars.embedColor)
.setColor(globalVars.embedColor)
.setDescription(`Today's most stannable person is ${candidateRandom.username}, everyone!`)
.setImage(randomGif);
channel.send({
Expand All @@ -42,7 +44,6 @@ module.exports = async (client) => {
}, timeZone = timezone, start = true);

} catch (e) {
// Log error
logger(e, client);
};
};
120 changes: 59 additions & 61 deletions bot.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,65 @@
let botjsFunction = async function botjsFunction() {
const Discord = require('discord.js');
const fs = require("fs");
const path = require("path");
// all except guild presence
// privileged: MessageContent, GuildMembers
const intents = [Discord.GatewayIntentBits.Guilds, Discord.GatewayIntentBits.GuildMembers, Discord.GatewayIntentBits.GuildBans, Discord.GatewayIntentBits.GuildEmojisAndStickers, Discord.GatewayIntentBits.GuildIntegrations, Discord.GatewayIntentBits.GuildVoiceStates, Discord.GatewayIntentBits.GuildMessages, Discord.GatewayIntentBits.GuildMessageReactions, Discord.GatewayIntentBits.DirectMessages, Discord.GatewayIntentBits.MessageContent];
const partials = [Discord.Partials.Channel, Discord.Partials.GuildMember, Discord.Partials.Message, Discord.Partials.Reaction, Discord.Partials.User];
import Discord from "discord.js";
import fs from 'fs';
import path from 'path';
import config from './config.json' with { type: "json" };

const client = new Discord.Client({
intents: intents,
partials: partials,
allowedMentions: { parse: ['users', 'roles'], repliedUser: true },
shards: "auto"
});
const config = require("./config.json");
client.config = config;
// This loop reads the /events/ folder and attaches each event file to the appropriate event.
fs.readdir("./events/", (err, files) => {
if (err) return console.error(err);
files.forEach(file => {
// If the file is not a JS file, ignore it.
if (!file.endsWith(".js")) return;
// Load the event file itself
const event = require(`./events/${file}`);
// Get just the event name from the file name
let eventName = file.split(".")[0];
// Each event will be called with the client argument,
// followed by its "normal" arguments, like message, member, etc.
client.on(eventName, event.bind(null, client));
delete require.cache[require.resolve(`./events/${file}`)];
});
// All except guild presence
// privileged: MessageContent, GuildMembers, GuildPresence
const intents = [Discord.GatewayIntentBits.Guilds, Discord.GatewayIntentBits.GuildMembers, Discord.GatewayIntentBits.GuildBans, Discord.GatewayIntentBits.GuildEmojisAndStickers, Discord.GatewayIntentBits.GuildIntegrations, Discord.GatewayIntentBits.GuildVoiceStates, Discord.GatewayIntentBits.GuildMessages, Discord.GatewayIntentBits.GuildMessageReactions, Discord.GatewayIntentBits.DirectMessages, Discord.GatewayIntentBits.MessageContent];
const partials = [Discord.Partials.Channel, Discord.Partials.GuildMember, Discord.Partials.Message, Discord.Partials.Reaction, Discord.Partials.User];

const client = new Discord.Client({
intents: intents,
partials: partials,
allowedMentions: { parse: ['users', 'roles'], repliedUser: true },
shards: "auto"
});
client.config = config;
// This loop reads the /events/ folder and attaches each event file to the appropriate event.
fs.readdir("./events/", (err, files) => {
if (err) return console.error(err);
files.forEach(async (file) => {
// If the file is not a JS file, ignore it.
if (!file.endsWith(".js")) return;
// Load the event file itself
let event = await import(`./events/${file}`);
event = event.default;
// Get just the event name from the file name
let eventName = file.split(".")[0];
// Each event will be called with the client argument,
// followed by its "normal" arguments, like message, member, etc.
client.on(eventName, event.bind(null, client));
});
client.commands = new Discord.Collection();
client.aliases = new Discord.Collection();
await walk(`./commands/`);
console.log("Loaded commands!");
});
client.commands = new Discord.Collection();
client.aliases = new Discord.Collection();
await walk(`./commands/`);
console.log("Loaded commands!");

client.login(config.token);
// This loop reads the /commands/ folder and attaches each command file to the appropriate command.
async function walk(dir, callback) {
fs.readdir(dir, function (err, files) {
if (err) throw err;
files.forEach(function (file) {
let filepath = path.join(dir, file);
fs.stat(filepath, function (err, stats) {
if (stats.isDirectory()) {
walk(filepath, callback);
} else if (stats.isFile() && file.endsWith('.js')) {
let props = require(`./${filepath}`);
if (!props.config.type) props.config.type = Discord.ApplicationCommandType.ChatInput;
let commandName = file.split(".")[0];
// console.log(`Loaded command: ${commandName} ✔`);
client.commands.set(commandName, props);
if (props.config.aliases) {
props.config.aliases.forEach(alias => {
if (client.aliases.get(alias)) return console.log(`Warning: Two commands share an alias name: ${alias}`);
client.aliases.set(alias, commandName);
});
};
client.login(config.token);
// This loop reads the /commands/ folder and attaches each command file to the appropriate command.
async function walk(dir, callback) {
fs.readdir(dir, function (err, files) {
if (err) throw err;
files.forEach(function (file) {
let filepath = path.join(dir, file);
fs.stat(filepath, async function (err, stats) {
if (stats.isDirectory()) {
walk(filepath, callback);
} else if (stats.isFile() && file.endsWith('.js')) {
let props = await import(`./${filepath}`);
if (!props.config.type) props.config.type = Discord.ApplicationCommandType.ChatInput;
let commandName = file.split(".")[0];
// console.log(`Loaded command: ${commandName} ✔`);
client.commands.set(commandName, props);
if (props.config.aliases) {
props.config.aliases.forEach(alias => {
if (client.aliases.get(alias)) return console.log(`Warning: Two commands share an alias name: ${alias}`);
client.aliases.set(alias, commandName);
});
};
});
};
});
});
};
};
botjsFunction();
});
};
20 changes: 12 additions & 8 deletions commands/api/dictionary.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
const Discord = require("discord.js");
exports.run = async (client, interaction, logger, ephemeral) => {
import Discord from "discord.js";
import logger from "../../util/logger.js";
import sendMessage from "../../util/sendMessage.js";
import globalVars from "../../objects/globalVars.json" with { type: "json" };
import axios from "axios";

export default async (client, interaction, ephemeral) => {
try {
const sendMessage = require('../../util/sendMessage');
const axios = require("axios");
let api = "https://api.dictionaryapi.dev/api/v2/";

let ephemeralArg = interaction.options.getBoolean("ephemeral");
if (ephemeralArg !== null) ephemeral = ephemeralArg;
await interaction.deferReply({ ephemeral: ephemeral });
let dictionaryEmbed = new Discord.EmbedBuilder()
.setColor(client.globalVars.embedColor);
.setColor(globalVars.embedColor);

let inputWord = interaction.options.getString("word");
let inputWordType = interaction.options.getString("wordtype");
let wordStatus;

try {
// Sometimes API doesn't respond when a word doesn't exist, sometimes it errors properly. Timeout is to catch both.
Expand All @@ -21,7 +25,8 @@ exports.run = async (client, interaction, logger, ephemeral) => {
new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout')), 5000))
]);
wordStatus = wordStatus.data;
} catch (error) {
} catch (e) {
// console.log(e);
let errorEmbed = new Discord.EmbedBuilder()
.setColor('#FF0000')
.setTitle("Error")
Expand Down Expand Up @@ -79,12 +84,11 @@ exports.run = async (client, interaction, logger, ephemeral) => {
return sendMessage({ client: client, interaction: interaction, embeds: dictionaryEmbed, ephemeral: ephemeral });

} catch (e) {
// Log error
logger(e, client, interaction);
};
};

module.exports.config = {
export const config = {
name: "dictionary",
description: `Get definition of a word.`,
options: [{
Expand Down
Loading

0 comments on commit e338e9c

Please sign in to comment.