DiscordTickets/src/utils/discord.js
Isaac 95f76716ed
feat: #206 convert to interactions (buttons and application commands) (#238)
* feat: make command handler slash command-ready

Only `help` and `settings` commands work so far

* feat(commands): finish new settings command

* fix(settings): convert `roles` and `ping` to an array

* fix(commands): make `add` a slash command

* fix(commands): make `blacklist` a slash command

* fix(commands): remove URLs from `help` command

* Add weblate badge and overview image

* Update sponsors

* sqlite things

* imrpovements

* update eslint rules

* (⚠ untested) close command

* fix: default locale for getting command option names

* Update README.md

* Update README.md

* Update README.md

* update new command to slash commands and fix close command

* fixes and improvements

* fix: closing a ticket when the creator has left

* Revert "fix: closing a ticket when the creator has left"

This reverts commit afc40ae17077782e344fd8cee03a089966c2347e.

* fix: closing a ticket when the creator has left

* fix: localisation issues in settings command

* fix: delete category channel

* New button and select panels + updated message panels

Includes new options for panel embed message image and thumbnail

Co-Authored-By: Puneet Gopinath <baalkrshna@gmail.com>
Co-Authored-By: thevisuales <6569806+thevisuales@users.noreply.github.com>

* Finish converting to buttons, added close button

Co-Authored-By: Puneet Gopinath <baalkrshna@gmail.com>
Co-Authored-By: thevisuales <6569806+thevisuales@users.noreply.github.com>

* fully convert to slash commands

* re-add "... has created a ticket" message

* locales

* fix add and remove commands

* fix remove command

* fix stats command

* eslint

Co-authored-by: Puneet Gopinath <baalkrshna@gmail.com>
Co-authored-by: thevisuales <6569806+thevisuales@users.noreply.github.com>
2021-09-24 15:32:36 +01:00

96 lines
2.1 KiB
JavaScript

const { GuildMember } = require('discord.js'); // eslint-disable-line no-unused-vars
const { Model } = require('sequelize'); // eslint-disable-line no-unused-vars
const config = require('../../user/config');
let current_presence = -1;
module.exports = class DiscordUtils {
constructor(client) {
this.client = client;
}
/**
* Generate embed footer text
* @param {string} text
* @param {string} [additional]
* @returns {string}
*/
footer(text, additional) {
if (text && additional) return `${text} | ${additional}`;
else return additional || text || '';
}
/**
* Check if a guild member is staff
* @param {GuildMember} member - the guild member
* @returns {boolean}
*/
async isStaff(member) {
const guild_categories = await this.client.db.models.Category.findAll({ where: { guild: member.guild.id } });
return guild_categories.some(cat => cat.roles.some(r => member.roles.cache.has(r)));
}
/**
* Fet a guild's settings
* @param {string} id - The guild's ID
* @returns {Promise<Model>}
*/
async getSettings(id) {
const data = { id };
const [settings] = await this.client.db.models.Guild.findOrCreate({
defaults: data,
where: data
});
return settings;
}
/**
* Delete a guild's settings
* @param {string} id - The guild ID
* @returns {Promise<Number>}
*/
async deleteSettings(id) {
const row = await this.getSettings(id);
return await row.destroy();
}
/**
* Select a presence from the config
* @returns {PresenceData}
*/
static selectPresence() {
const length = config.presence.presences.length;
if (length === 0) return {};
let num;
if (length === 1) {
num = 0;
} else if (config.presence.randomise) {
num = Math.floor(Math.random() * length);
} else {
current_presence = current_presence + 1; // ++ doesn't work on negative numbers
if (current_presence === length) {
current_presence = 0;
}
num = current_presence;
}
const {
activity: name,
status,
type,
url
} = config.presence.presences[num];
return {
activities: [
{
name,
type,
url
}
],
status
};
}
};