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

Commit

Permalink
Added embed service & commands | v1.0.5-beta
Browse files Browse the repository at this point in the history
  • Loading branch information
CrawlTheDev committed Jun 8, 2021
1 parent e41d6e1 commit 1df914a
Show file tree
Hide file tree
Showing 19 changed files with 342 additions and 86 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pbot-plus/bot",
"version": "1.0.4-beta",
"version": "1.0.5-beta",
"description": "A multi-purpose Discord bot with Typescript",
"main": "src/main.ts",
"scripts": {
Expand Down
69 changes: 42 additions & 27 deletions src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import { Constants, Guild, Message, MessageEmbed } from 'discord.js-light';
import { Constants, Guild, Message } from 'discord.js-light';

import { CustomClient } from './extensions';
import { DatabaseProvider, GuildModel } from './providers';
import {
LoggerTypes,
LoggerType,
LoggerService,
ConfigService,
ConfigTypes,
CommandService
ConfigType,
CommandService,
EmbedService
} from './services';

export class PbotPlus {
/**
* Custom logger for Pbot-plus
*/
public readonly logger: LoggerTypes = LoggerService;
public readonly logger: LoggerType = LoggerService;

/**
* Configuration files for Pbot-plus
*/
public readonly config: ConfigTypes = ConfigService;
public readonly config: ConfigType = ConfigService;

/**
* Embed service
*/
public embed: EmbedService = new EmbedService(this);

/**
* Database provider
Expand All @@ -29,7 +35,7 @@ export class PbotPlus {
/**
* Command service
*/
public commands: CommandService;
public commandService: CommandService;

/**
* @param bot Client
Expand Down Expand Up @@ -81,7 +87,6 @@ export class PbotPlus {
useNewUrlParser: true,
useUnifiedTopology: true
});

await database.initialize();
this.database = database;
} catch (error) {
Expand All @@ -95,9 +100,8 @@ export class PbotPlus {
private registerCommands(): void {
try {
const commandService = new CommandService(this);

commandService.initialize();
this.commands = commandService;
this.commandService = commandService;
} catch (error) {
this.logger.error('Failed while registering commands', error);
}
Expand All @@ -110,18 +114,24 @@ export class PbotPlus {
private async onMessage(message: Message): Promise<void> {
if (message.author.bot ?? !message.guild?.member) return;

const commandPrefix =
(await this.database.findGuildById(message.guild.id))?.main.prefix ??
this.config.client.defaultPrefix;

if (message.content.match(new RegExp(`^<@!?${this.bot.user?.id}>( |)$`))) {
const PrefixEmbed = new MessageEmbed({
color: this.config.color.info,
description: `Command prefix: ${commandPrefix}`
});
message.channel.send(PrefixEmbed);
} else if (message.content.startsWith(commandPrefix)) {
await this.commands.run(message);
const guild = await this.database.findGuildById(message.guild.id);
const commandPrefix = guild?.main.prefix;

if (guild) {
if (message.content.match(new RegExp(`^<@!?${this.bot.user.id}>( |)$`))) {
const prefixEmbed = this.embed.info(true, {
description: `My command prefix on this server: \`${commandPrefix}\``
});
await message.channel.send(prefixEmbed);
} else if (message.content.startsWith(commandPrefix)) {
await this.commandService.run(message);
}
} else {
await message.channel.send(
this.embed.error(
`Guild with id **${message.guild.id}** not found in database`
)
);
}
}

Expand Down Expand Up @@ -153,10 +163,15 @@ export class PbotPlus {
await this.database.fetchNewGuilds();
}

this.logger.info(`Signed in as ${this.bot.user?.tag}`);
this.bot.setPresence(
'WATCHING',
`to ${this.bot.guilds.cache.size.toLocaleString()} guilds | @PBOT`
);
if (this.config.client.presence) {
await this.bot.setPresence(
'WATCHING',
`to ${this.bot.guilds.cache.size.toLocaleString()} guilds | @${
this.bot.user.username
}`
);
}

this.logger.info(`Signed in as ${this.bot.user.tag}`);
}
}
25 changes: 20 additions & 5 deletions src/commands/admin/change-prefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { ICommand, CommandContext } from '..';

export default class ChangePrefixCommand implements ICommand {
name = 'change-prefix';
description = "Allows you to change the bot's prefix";
example = 'change-prefix <prefix>';
aliases = ['onek-degistir'];

async execute(ctx: CommandContext, prefix: string): Promise<void> {
if (ctx.member.hasPermission('ADMINISTRATOR')) {
Expand All @@ -11,24 +14,36 @@ export default class ChangePrefixCommand implements ICommand {
if (prefix) {
if (prefix === guild.main.prefix) {
await ctx.channel.send(
'Please provide a different prefix than the previous prefix'
ctx.embed.error(
'Please provide a different prefix than the previous prefix'
)
);
} else {
guild.main.prefix = prefix;
await guild.save();
await ctx.channel.send('Prefix changed to: ' + guild?.main.prefix);

const PrefixChangedEmbed = ctx.embed.success(
`My command prefix changed to: \`${guild?.main.prefix}\``
);
await ctx.channel.send(PrefixChangedEmbed);
}
} else {
await ctx.channel.send('Please provide a valid prefix');
await ctx.channel.send(
ctx.embed.warn('Please provide a prefix to replace my prefix')
);
}
} else {
await ctx.channel.send(
`Guild with id **'${ctx.guild.id}'** not found in database`
ctx.embed.error(
`Guild with id **${ctx.guild.id}** not found in database`
)
);
}
} else {
await ctx.channel.send(
"You don't have permission(`ADMINISTRATOR`) for use this command"
ctx.embed.error(
"You don't have permission(`ADMINISTRATOR`) to use this command"
)
);
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/commands/command.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ export interface ICommand {
*/
name: string;

/**
* Command description
*/
description?: string;

/**
* Command example
*/
example?: string;

/**
* Command aliases
*/
aliases?: Array<string>;

/**
* Execute command
*/
Expand Down
24 changes: 15 additions & 9 deletions src/commands/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ import {
import { ICommand } from './command.interface';
import { CustomClient } from '../extensions';
import { PbotPlus } from '../bot';
import { ConfigTypes, LoggerTypes } from '../services';
import { ConfigType, LoggerType, EmbedService } from '../services';
import { DatabaseProvider } from '../providers';

export class CommandContext {
/**
* Embed
*/
public embed: EmbedService;

/**
* Database
*/
Expand All @@ -21,12 +26,12 @@ export class CommandContext {
/**
* Logger
*/
public logger: LoggerTypes;
public logger: LoggerType;

/**
* Config
*/
public config: ConfigTypes;
public config: ConfigType;

/**
* Guild member
Expand Down Expand Up @@ -64,18 +69,19 @@ export class CommandContext {
* @param command
*/
constructor(
pbot: PbotPlus,
protected _pbot: PbotPlus,
public message: Message,
public command: ICommand
) {
this.member = message.member;
this.channel = message.channel as TextChannel;
this.guild = message.guild;
this.user = message.member.user;
this.pbot = pbot;
this.bot = pbot.bot;
this.config = pbot.config;
this.logger = pbot.logger;
this.database = pbot.database;
this.pbot = _pbot;
this.bot = _pbot.bot;
this.config = _pbot.config;
this.logger = _pbot.logger;
this.database = _pbot.database;
this.embed = _pbot.embed;
}
}
80 changes: 80 additions & 0 deletions src/commands/core/help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { ICommand, CommandContext } from '..';

export default class HelpCommand implements ICommand {
name = 'help';
description = 'Shows all commands';
example = 'help [command-name]';
aliases = ['yardım'];

async execute(ctx: CommandContext, commandName: string): Promise<void> {
const guild = await ctx.database.findGuildById(ctx.guild.id);

if (guild) {
if (!commandName) {
const HelpEmbed = ctx.embed.info(true, {
title: 'Command Page',
description: 'The commands you can use are listed',
fields: [
{
name: 'Admin',
value: '`change-prefix`',
inline: true
},
{
name: 'Core',
value: '`invite`**,** `help`',
inline: true
}
]
});
await ctx.channel.send(HelpEmbed);
} else {
const commands = ctx.pbot.commandService.commands;
const command =
commands.get(commandName) ??
Array.from(commands.values()).find((c) =>
c.aliases?.some((a) => a === commandName)
);

if (command) {
const commandDescription =
command.description ?? "I couldn't find the command description";
const commandExample =
command.example ?? "I couldn't find the command example";
const commandAliases =
command.aliases?.map((alias) => `\`${alias}\``).join('**,** ') ??
"I couldn't find the command aliases";
const commandName = command.name;

const CommandInfoEmbed = ctx.embed.info(true, {
title: `[HELP] Command: ${commandName.toUpperCase()}`,
description: commandDescription,
fields: [
{
name: 'Example Usage',
value: command.example
? `\`${guild?.main.prefix}${commandExample}\``
: commandExample
},
{
name: 'Aliases',
value: commandAliases
}
]
});
await ctx.channel.send(CommandInfoEmbed);
} else {
await ctx.channel.send(
ctx.embed.error("I couldn't find this command")
);
}
}
} else {
await ctx.channel.send(
ctx.embed.error(
`Guild with id **${ctx.guild.id}** not found in database`
)
);
}
}
}
16 changes: 8 additions & 8 deletions src/commands/core/invite.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { MessageEmbed } from 'discord.js';

import { ICommand, CommandContext } from '..';

export default class InviteCommand implements ICommand {
name = 'invite';
description = 'Allows you to add the bot';
example = 'invite';
aliases = ['davet-et'];

async execute(ctx: CommandContext): Promise<void> {
const invite_url = `https://discord.com/oauth2/authorize?client_id=${ctx.bot.user.id}&scope=bot&permissions=8`;
const InviteEmbed = new MessageEmbed({
color: ctx.config.color.info,
description: `Hello, [click here](${invite_url}) to invite me to guild`
});

await ctx.channel.send(InviteEmbed);
await ctx.channel.send(
ctx.embed.info(false, {
description: `Hello, [click here](${invite_url}) to invite me to guild`
})
);
}
}
9 changes: 3 additions & 6 deletions src/providers/database/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export class DatabaseProvider {
*/
public async findGuildById(id: string): Promise<IGuildModel | null> {
const guild = await GuildModel.findById(id);

return guild ?? null;
}

Expand All @@ -105,11 +104,9 @@ export class DatabaseProvider {
private getConnectionUri(): string {
const config = this.pbot.config.database;

if (config.local) {
return `mongodb://${config.hostname}:${config.port}/${config.base}`;
} else {
return `mongodb+srv://${config.username}:${config.password}@${config.hostname}/${config.base}?retryWrites=true&w=majority`;
}
return config.local
? `mongodb://${config.hostname}:${config.port}/${config.base}`
: `mongodb+srv://${config.username}:${config.password}@${config.hostname}/${config.base}?retryWrites=true&w=majority`;
}

/**
Expand Down
Loading

0 comments on commit 1df914a

Please sign in to comment.