Skip to content

Commit

Permalink
feat: add blacklists (#81)
Browse files Browse the repository at this point in the history
* Create blacklist.ts

* Create blacklist.ts

* Create blacklist.ts

* Create blacklist.ts

* feat: start with blacklist command

* feat: command shortening

* fix: ts errors

* feat: move back to cjs

* feat: add blacklist command

* feat: add CI (#84)

* Create commit-msg

* Create pre-commit

* Create .commitlintrc.json

* Create .editorconfig

* Create .eslintrc.json

* Create .eslintignore

* Create .gitattributes

* Create tsconfig.eslint.json

* Update package.json

* Create .lintstagedrc.json

* Create eslint.json

* Create tsc.json

* Create continuous-integration.yml

* Update package.json

* Create extensions.json

* Create settings.json

* feat: update lock file

* feat: add eslint-plugin-prettier

* fix: eslint errors

* fix: more eslint errors

* Create blacklist.ts

* feat: start with blacklist command

* feat: command shortening

* fix: ts errors

* feat: move back to cjs

* feat: add blacklist command

* fix: eslint errors

* feat: NotBlacklisted

* fix: eslint errors

* feat: add NotBlacklisted precondition to commands

* fix: ts errors

* fix: ts errors

* feat: add blacklisted check

Co-authored-by: Jaron <jaron_zijlstra@outlook.com>
Co-authored-by: Jaron <60853956+JaronZ@users.noreply.github.com>
  • Loading branch information
3 people committed Apr 7, 2022
1 parent 46201a7 commit e227f64
Show file tree
Hide file tree
Showing 22 changed files with 129 additions and 20 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ourtube",
"version": "1.0.0",
"description": "A music discord bot written in discord.js",
"main": "dist/sharder.mjs",
"main": "dist/sharder.js",
"directories": {
"lib": "src"
},
Expand All @@ -15,8 +15,8 @@
"build": "yarn clean && tsup && tsc --p tsconfig.lang.json",
"lint": "eslint src scripts --ext mjs,ts --fix",
"format": "prettier --write **/*.{ts,js,json,yml,yaml}",
"prepare": "husky install",
"start": "yarn build && node dist/sharder.mjs"
"prepare": "husky install",
"start": "yarn build && node ."
},
"repository": {
"type": "git",
Expand Down
2 changes: 2 additions & 0 deletions src/augment.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type {PrismaClient} from "@prisma/client";
import {Player} from "discord-player";

export * from "@sapphire/pieces";
declare module "@sapphire/pieces" {
export interface Container {
player: Player;
prisma: PrismaClient;
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import {container, LogLevel, SapphireClient} from "@sapphire/framework";
import "@sapphire/plugin-logger/register";
import "@sapphire/plugin-i18next/register";
import "./container";
import {PrismaClient} from "@prisma/client";
config();

const client = new SapphireClient({
intents: ["GUILDS", "GUILD_VOICE_STATES"]
});
container.player = new Player(client);
container.logger = new Logger(container, {level: LogLevel.Debug});
container.prisma = new PrismaClient();

void client.login(process.env.TOKEN);
void client.login(process.env.TOKEN).finally(() => container.prisma.$disconnect());
60 changes: 60 additions & 0 deletions src/commands/blacklist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {SlashCommandBuilder} from "@discordjs/builders";
import {ApplyOptions} from "@sapphire/decorators";
import type {ApplicationCommandRegistry, CommandOptions} from "@sapphire/framework";
import {CommandInteraction, MessageEmbed} from "discord.js";
import {Command} from "../lib/Command";

@ApplyOptions<CommandOptions>({
description: "blacklist"
})
export class BlacklistCommand extends Command {
public registerApplicationCommands(registry: ApplicationCommandRegistry) {
registry.registerChatInputCommand(
new SlashCommandBuilder()
.setName(this.name)
.setDescription(this.description)
.addUserOption(input => input.setName("user").setDescription("the user to blacklist").setRequired(true))
.addStringOption(input =>
input.setName("reason").setDescription("reason why for being blacklisted").setRequired(true)
),
{
guildIds: ["863878432697614337"]
}
);
}

public async chatInputRun(interaction: CommandInteraction) {
await interaction.deferReply();

const user = interaction.options.getUser("user", true);
const reason = interaction.options.getString("reason", true);

if (await this.container.prisma.blacklist.findUnique({where: {user: user.id}})) {
return interaction.editReply({
embeds: [
new MessageEmbed()
.setColor("RED")
.setTitle("User already blacklisted")
.setDescription(`${user} is already blacklisted.`)
]
});
}

await this.container.prisma.blacklist.create({
data: {
user: user.id,
reason,
author: interaction.user.id
}
});

return interaction.editReply({
embeds: [
new MessageEmbed()
.setColor("GREEN")
.setTitle("User blacklisted")
.setDescription(`${user} has been blacklisted`)
]
});
}
}
4 changes: 3 additions & 1 deletion src/commands/clear.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {SlashCommandBuilder} from "@discordjs/builders";
import {ApplyOptions} from "@sapphire/decorators";
import {ApplicationCommandRegistry, Command, CommandOptions} from "@sapphire/framework";
import type {ApplicationCommandRegistry, CommandOptions} from "@sapphire/framework";
import {CommandInteraction, MessageEmbed} from "discord.js";
import {Command} from "../lib/Command";

@ApplyOptions<CommandOptions>({
description: "clear the queue",
preconditions: ["GuildOnly", "BotInVoice", "InSameVoice"]
Expand Down
3 changes: 2 additions & 1 deletion src/commands/disconnect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {SlashCommandBuilder} from "@discordjs/builders";
import {ApplyOptions} from "@sapphire/decorators";
import {ApplicationCommandRegistry, Command, CommandOptions} from "@sapphire/framework";
import type {ApplicationCommandRegistry, CommandOptions} from "@sapphire/framework";
import {CommandInteraction, MessageEmbed} from "discord.js";
import {Command} from "../lib/Command";

@ApplyOptions<CommandOptions>({
description: "let the bot disconnect from the currently joined voice channel",
Expand Down
3 changes: 2 additions & 1 deletion src/commands/invite.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {SlashCommandBuilder} from "@discordjs/builders";
import {ApplyOptions} from "@sapphire/decorators";
import {ApplicationCommandRegistry, Command, CommandOptions} from "@sapphire/framework";
import type {ApplicationCommandRegistry, CommandOptions} from "@sapphire/framework";
import {stripIndents} from "common-tags";
import {CommandInteraction, MessageEmbed} from "discord.js";
import {Command} from "../lib/Command";

@ApplyOptions<CommandOptions>({
description: "get the invite link of the bot"
Expand Down
3 changes: 2 additions & 1 deletion src/commands/join.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {SlashCommandBuilder} from "@discordjs/builders";
import {ApplyOptions} from "@sapphire/decorators";
import {ApplicationCommandRegistry, Command, CommandOptions} from "@sapphire/framework";
import type {ApplicationCommandRegistry, CommandOptions} from "@sapphire/framework";
import {CommandInteraction, GuildMember, MessageEmbed} from "discord.js";
import {Command} from "../lib/Command";

@ApplyOptions<CommandOptions>({
description: "let the bot join your voice channel",
Expand Down
3 changes: 2 additions & 1 deletion src/commands/nowPlaying.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {SlashCommandBuilder} from "@discordjs/builders";
import {ApplyOptions} from "@sapphire/decorators";
import {ApplicationCommandRegistry, Command, CommandOptions} from "@sapphire/framework";
import type {ApplicationCommandRegistry, CommandOptions} from "@sapphire/framework";
import {stripIndents} from "common-tags";
import {CommandInteraction, MessageEmbed} from "discord.js";
import {Command} from "../lib/Command";

@ApplyOptions<CommandOptions>({
description: "show the current playing song",
Expand Down
3 changes: 2 additions & 1 deletion src/commands/pause.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {SlashCommandBuilder} from "@discordjs/builders";
import {ApplyOptions} from "@sapphire/decorators";
import {ApplicationCommandRegistry, Command, CommandOptions} from "@sapphire/framework";
import type {ApplicationCommandRegistry, CommandOptions} from "@sapphire/framework";
import {CommandInteraction, MessageEmbed} from "discord.js";
import {Command} from "../lib/Command";

@ApplyOptions<CommandOptions>({
description: "pause the current song",
Expand Down
3 changes: 2 additions & 1 deletion src/commands/play.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {SlashCommandBuilder} from "@discordjs/builders";
import {ApplyOptions} from "@sapphire/decorators";
import {ApplicationCommandRegistry, Command, CommandOptions, SapphireClient} from "@sapphire/framework";
import type {ApplicationCommandRegistry, CommandOptions, SapphireClient} from "@sapphire/framework";
import {QueryType} from "discord-player";
import {CommandInteraction, MessageEmbed} from "discord.js";
import type {JoinCommand} from "./join";
import {Command} from "../lib/Command";

@ApplyOptions<CommandOptions>({
description: "play a song",
Expand Down
3 changes: 2 additions & 1 deletion src/commands/queue.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {SlashCommandBuilder} from "@discordjs/builders";
import {ApplyOptions} from "@sapphire/decorators";
import {ApplicationCommandRegistry, Command, CommandOptions} from "@sapphire/framework";
import type {ApplicationCommandRegistry, CommandOptions} from "@sapphire/framework";
import {CommandInteraction, MessageEmbed} from "discord.js";
import {Command} from "../lib/Command";

@ApplyOptions<CommandOptions>({
description: "show the current queue",
Expand Down
3 changes: 2 additions & 1 deletion src/commands/remove.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {SlashCommandBuilder} from "@discordjs/builders";
import {ApplyOptions} from "@sapphire/decorators";
import {ApplicationCommandRegistry, Command, CommandOptions} from "@sapphire/framework";
import type {ApplicationCommandRegistry, CommandOptions} from "@sapphire/framework";
import {CommandInteraction, MessageEmbed} from "discord.js";
import {Command} from "../lib/Command";

@ApplyOptions<CommandOptions>({
description: "remove a song from the current queue",
Expand Down
3 changes: 2 additions & 1 deletion src/commands/resume.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {SlashCommandBuilder} from "@discordjs/builders";
import {ApplyOptions} from "@sapphire/decorators";
import {ApplicationCommandRegistry, Command, CommandOptions} from "@sapphire/framework";
import type {ApplicationCommandRegistry, CommandOptions} from "@sapphire/framework";
import {CommandInteraction, MessageEmbed} from "discord.js";
import {Command} from "../lib/Command";

@ApplyOptions<CommandOptions>({
description: "resume the current song",
Expand Down
3 changes: 2 additions & 1 deletion src/commands/seek.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {SlashCommandBuilder} from "@discordjs/builders";
import {ApplyOptions} from "@sapphire/decorators";
import {ApplicationCommandRegistry, Command, CommandOptions} from "@sapphire/framework";
import type {ApplicationCommandRegistry, CommandOptions} from "@sapphire/framework";
import {CommandInteraction, MessageEmbed} from "discord.js";
import {Duration} from "@sapphire/time-utilities";
import {stripIndents} from "common-tags";
import {Command} from "../lib/Command";

@ApplyOptions<CommandOptions>({
description: "seek to a specific time in the current song"
Expand Down
3 changes: 2 additions & 1 deletion src/commands/skip.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {SlashCommandBuilder} from "@discordjs/builders";
import {ApplyOptions} from "@sapphire/decorators";
import {ApplicationCommandRegistry, Command, CommandOptions} from "@sapphire/framework";
import type {ApplicationCommandRegistry, CommandOptions} from "@sapphire/framework";
import {CommandInteraction, MessageEmbed} from "discord.js";
import {Command} from "../lib/Command";

@ApplyOptions<CommandOptions>({
description: "skip the current song"
Expand Down
3 changes: 2 additions & 1 deletion src/commands/support.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {SlashCommandBuilder} from "@discordjs/builders";
import {ApplyOptions} from "@sapphire/decorators";
import {ApplicationCommandRegistry, Command, CommandOptions} from "@sapphire/framework";
import type {ApplicationCommandRegistry, CommandOptions} from "@sapphire/framework";
import {CommandInteraction, MessageEmbed} from "discord.js";
import {Command} from "../lib/Command";

@ApplyOptions<CommandOptions>({
description: "get the invite link of the support server"
Expand Down
3 changes: 2 additions & 1 deletion src/commands/volume.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {ApplyOptions} from "@sapphire/decorators";
import {ApplicationCommandRegistry, Command, CommandOptions} from "@sapphire/framework";
import type {ApplicationCommandRegistry, CommandOptions} from "@sapphire/framework";
import {CommandInteraction, MessageEmbed} from "discord.js";
import {Command} from "../lib/Command";

@ApplyOptions<CommandOptions>({
description: "set the volume of the player",
Expand Down
10 changes: 10 additions & 0 deletions src/lib/Command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {Command as SapphireCommand, PieceContext} from "@sapphire/framework";

export abstract class Command extends SapphireCommand {
public constructor(context: PieceContext, options: SapphireCommand.Options) {
super(context, {
...options,
preconditions: [...(options.preconditions ?? []), "NotBlacklisted"]
});
}
}
20 changes: 20 additions & 0 deletions src/preconditions/NotBlacklisted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {Precondition} from "@sapphire/framework";
import type {CommandInteraction} from "discord.js";

export class NotBlacklistedPrecondition extends Precondition {
public async chatInputRun(interaction: CommandInteraction) {
return (await this.container.prisma.blacklist.findUnique({
where: {
user: interaction.user.id
}
}))
? this.error({message: "You are blacklisted"})
: this.ok();
}
}

declare module "@sapphire/framework" {
interface Preconditions {
NotBlacklisted: never;
}
}
2 changes: 1 addition & 1 deletion src/sharder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const logger = new Logger(container, {

const logShardEvent = (shard: Shard, event: string) => logger.debug(`Shard ${shard.id} ${event}`);

new ShardingManager(join(__dirname, "bot.mjs"), {
new ShardingManager(join(__dirname, "bot.js"), {
token: process.env.TOKEN,
mode: "worker"
})
Expand Down
2 changes: 1 addition & 1 deletion tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const tsup: Options = {
clean: true,
dts: false,
entryPoints: ["src/sharder.ts", "src/bot.ts", "src/commands/**/*.ts", "src/listeners/**/*.ts", "src/preconditions/**/*.ts"],
format: ["esm"],
format: ["cjs"],
minify: true,
skipNodeModulesBundle: true,
sourcemap: false,
Expand Down

0 comments on commit e227f64

Please sign in to comment.