Private chat
- added commands to the private chat - added Guild Utility commandspull/1/head
parent
8843b40e8c
commit
6c84aefd96
@ -1 +1,3 @@
|
||||
2b-bot-2
|
||||
# 2B Bot 2
|
||||
|
||||
A typescript rewrite of my discordbot 2b.
|
||||
|
@ -1,4 +1,6 @@
|
||||
import {CommandCollection} from "../../lib/CommandCollection";
|
||||
import {Ping} from "./utility/Ping";
|
||||
|
||||
export const globalCommands = new CommandCollection([
|
||||
Ping,
|
||||
]);
|
||||
|
@ -0,0 +1,17 @@
|
||||
import {Command} from "../../../lib/Command";
|
||||
import {CommandPermission} from "../../../lib/CommandPermission";
|
||||
import {Message} from "discord.js";
|
||||
|
||||
export class Ping extends Command {
|
||||
public static commandName = "ping";
|
||||
public static permission = CommandPermission.REGULAR;
|
||||
public static description = "Replies with the bots ping.";
|
||||
|
||||
/**
|
||||
* Replies with the current ping.
|
||||
* @param msg
|
||||
*/
|
||||
public invoke(msg: Message): void {
|
||||
msg.channel.send(`My latency is **${Math.round(this.bot.client.ping)}** ms.`);
|
||||
}
|
||||
}
|
@ -1,6 +1,10 @@
|
||||
import {CommandCollection} from "../../lib/CommandCollection";
|
||||
import {AddAdminRoles} from "./utility/AddAdminRoles";
|
||||
import {SetPrefix} from "./utility/SetPrefix";
|
||||
import {RemoveAdminRoles} from "./utility/RemoveAdminRoles";
|
||||
|
||||
export const guildCommands = new CommandCollection([
|
||||
AddAdminRoles,
|
||||
SetPrefix,
|
||||
RemoveAdminRoles,
|
||||
]);
|
||||
|
@ -0,0 +1,24 @@
|
||||
import {CommandPermission} from "../../../lib/CommandPermission";
|
||||
import {GuildCommand} from "../../../lib/GuildCommand";
|
||||
import {Message} from "discord.js";
|
||||
|
||||
export class RemoveAdminRoles extends GuildCommand {
|
||||
public static commandName = "removeAdminRoles";
|
||||
public static description = "Removes one or more roles from the configured roles";
|
||||
public static permission = CommandPermission.ADMIN;
|
||||
|
||||
/**
|
||||
* Removes all specified roles from the admin roles.
|
||||
*/
|
||||
public invoke(msg: Message): Promise<void> | void {
|
||||
const args = RemoveAdminRoles.getArgs(msg.content);
|
||||
if (args.length < 2) {
|
||||
msg.channel.send("No argument for role names provided.");
|
||||
} else {
|
||||
const roles = args.splice(1);
|
||||
const adminRoles = this.guildHandler.settings.adminRoles;
|
||||
this.guildHandler.settings.adminRoles = adminRoles.filter((role) => !roles.includes(role));
|
||||
msg.channel.send(`Removed **${roles.join("**, **")}** from the admin roles.`);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
import {GuildCommand} from "../../../lib/GuildCommand";
|
||||
import {CommandPermission} from "../../../lib/CommandPermission";
|
||||
import {Message} from "discord.js";
|
||||
|
||||
export class SetPrefix extends GuildCommand {
|
||||
public static commandName = "setPrefix";
|
||||
public static permission = CommandPermission.ADMIN;
|
||||
|
||||
/**
|
||||
* Sets the prefix for a guild.
|
||||
* @param msg
|
||||
*/
|
||||
public invoke(msg: Message): Promise<void> | void {
|
||||
const args = SetPrefix.getArgs(msg.content);
|
||||
if (args.length > 1) {
|
||||
const prefix: string = args[1];
|
||||
if (/^\S+$/.test(prefix)) {
|
||||
this.guildHandler.settings.prefix = prefix;
|
||||
msg.channel.send(`Changed the command prefix to **${prefix}**`);
|
||||
} else {
|
||||
msg.channel.send(`**${prefix}** Is not a valid prefix. \nA prefix must be a non-whitespace sequence of characters.`);
|
||||
}
|
||||
} else {
|
||||
msg.channel.send("You need to provide a prefix as commadn argument.");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
import {Message} from "discord.js";
|
||||
import {CommandPermission} from "../CommandPermission";
|
||||
import {CommandCollection} from "../CommandCollection";
|
||||
import {Command} from "../Command";
|
||||
|
||||
/**
|
||||
* Parses a message and returns the corresponding command.
|
||||
* @param message
|
||||
* @param commandCollection
|
||||
* @param prefix
|
||||
* @param userPermission
|
||||
*/
|
||||
export function parseMessage(message: Message, commandCollection: CommandCollection<any>,
|
||||
prefix: string, userPermission: CommandPermission = 0): any {
|
||||
|
||||
const commandPattern = new RegExp( `\\s*${prefix}(\\w+)`);
|
||||
|
||||
if (commandPattern.test(message.content)) {
|
||||
const commandString = commandPattern.exec(message.content)[1];
|
||||
const CommandClass = commandCollection.findByName(commandString);
|
||||
|
||||
if (CommandClass) {
|
||||
if (CommandClass.permission <= userPermission) {
|
||||
return CommandClass;
|
||||
} else {
|
||||
message.channel.send("You don't have permission for that command.");
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
Loading…
Reference in New Issue