Skip to content

Commit

Permalink
feat: Change env configuration to config.js
Browse files Browse the repository at this point in the history
  • Loading branch information
hmes98318 committed Jan 4, 2025
1 parent dcc6c5c commit 69ffe87
Show file tree
Hide file tree
Showing 40 changed files with 402 additions and 283 deletions.
46 changes: 0 additions & 46 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,48 +1,2 @@
# Discord Bot Token
BOT_TOKEN = "your_token"

# Admin of the bot (User ID)
# For multiple admins, separate their user IDs with commas.
# OAUTH2 mode requires setting BOT_ADMIN, BOT_CLIENT_SECRET value
BOT_ADMIN = ""
BOT_CLIENT_SECRET = ""

# Bot settings
BOT_NAME = "Music Disc"
BOT_PREFIX = "+"
BOT_STATUS = "online"
BOT_PLAYING = "+help | music"
BOT_EMBEDS_COLOR = "#FFFFFF"
BOT_SLASH_COMMAND = true


# Volume settings
DEFAULT_VOLUME = 50
MAX_VOLUME = 100

# Auto leave channel settings
AUTO_LEAVE = true
AUTO_LEAVE_COOLDOWN = 5000

# Show voice channel updates
DISPLAY_VOICE_STATE = true


# Web dashboard settings
ENABLE_SITE = true
SITE_PORT = 33333
SITE_LOGIN_TYPE = "USER" # "USER" | "OAUTH2"

# USER mode settings
SITE_USERNAME = "admin"
SITE_PASSWORD = "000"

# OAUTH2 mode settings
SITE_OAUTH2_LINK = "" # Your OAuth2 authentication link
SITE_OAUTH2_REDIRECT_URI = "http://localhost:33333/login" # Redirect link after OAuth2 authentication is complete


# Local Lavalink node
ENABLE_LOCAL_NODE = false
LOCAL_NODE_AUTO_RESTART = true
# LOCAL_NODE_DOWNLOAD_LINK = ""
71 changes: 71 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* @type {import('./src/@types/index').Config} - Bot config
*/
const config = {
bot: {
textCommand : true, // Whether to enable text command
slashCommand : true, // Whether to enable slash command

// OAUTH2 mode requires setting 'admin', 'clientSecret' value
admin : [], // It must be the user ID (string[])
clientSecret : '',

name : 'Music Disc',
prefix : '-',
status : 'online', // 'online' | 'idle' | 'dnd'
activityType : 2, // https://discord.com/developers/docs/topics/gateway-events#activity-object-activity-types
playing : '+help | music',
embedsColor : '#FFFFFF',
volume: {
default : 50,
max : 100,
},
// Auto leave channel settings
autoLeave: {
enabled : true,
cooldown : 5000, // ms
},
// Show voice channel updates
displayVoiceState : true,
},

// Web dashboard settings
webDashboard: {
enabled : true,
port : 33333,
loginType : 'USER', // 'USER' | 'OAUTH2'

// USER mode settings
user: {
username : 'admin',
password : 'password',
},

// OAUTH2 mode settings
oauth2: {
link : '',
redirectUri : 'http://localhost:33333/login',
},

// SessionManager config
sessionManager: {
validTime : 10 * 60 * 1000, // Session validity time (ms) (default: 10 minutes)
cleanupInterval : 5 * 60 * 1000 // Timing cleaner time (ms) (default: 5 minutes)
},
// IPBlocker config
ipBlocker: {
retryLimit : 5, // Maximum number of retries (default: 5)
unlockTimeoutDuration : 5 * 60 * 1000, // Blocking time (ms) (default: 5 minutes)
cleanupInterval : 5 * 60 * 1000 // Timing cleaner time (ms) (default: 5 minutes)
}
},

// Local Lavalink node
localNode: {
enabled : false,
autoRestart : true,
// downloadLink : 'https://github.com/lavalink-devs/Lavalink/releases/download/4.0.8/Lavalink.jar'
}
};

export { config };
80 changes: 49 additions & 31 deletions src/@types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {
ActivityType,
ChatInputCommandInteraction,
ClientPresenceStatus,
Collection,
Expand Down Expand Up @@ -38,13 +39,15 @@ export interface QueuePage {
}


export enum LoginType {
export enum LoginTypeEnum {
USER = 'USER',
OAUTH2 = 'OAUTH2'
OAUTH2 = 'OAUTH2',
}

export type LoginType = keyof typeof LoginTypeEnum;

export interface Bot {

export type Bot = {
shardId: number;
blacklist: string[];
config: Config;
Expand All @@ -60,43 +63,58 @@ export interface Bot {
/**
* Constants variables
*/
export interface Config {
export type Config = {
bot: BotConfig;
webDashboard: WebDashboardConfig;
localNode: LocalNodeConfig;
};

export type BotConfig = {
textCommand: boolean;
slashCommand: boolean;
admin: string[];
clientSecret: string | null;
clientSecret: string;
name: string;
prefix: string;
status: ClientPresenceStatus | string;
status: ClientPresenceStatus;
activityType: ActivityType;
playing: string;
embedsColor: HexColorString | string | number;
slashCommand: boolean;
defaultVolume: number;
maxVolume: number;
autoLeave: boolean;
autoLeaveCooldown: number;
embedsColor: string;
volume: {
default: number;
max: number;
};
autoLeave: {
enabled: boolean;
cooldown: number;
};
displayVoiceState: boolean;
enableSite: boolean;
site: SiteConfig;
enableLocalNode: boolean;
localNode: LocalNode;
sessionManager: SessionManagerConfig;
ipBlocker: IPBlockerConfig;
}
};

interface SiteConfig {
export type WebDashboardConfig = {
enabled: boolean;
port: number;
loginType: 'USER' | 'OAUTH2';
username: string;
password: string;
oauth2Link: string | null;
oauth2RedirectUri: string | null;
}
loginType: LoginType;
user: {
username: string;
password: string;
};
oauth2: {
link: string;
redirectUri: string;
};
sessionManager: SessionManagerConfig;
ipBlocker: IPBlockerConfig;
};

interface LocalNode {
export type LocalNodeConfig = {
enabled: boolean;
autoRestart: boolean;
downloadLink: string;
}
downloadLink?: string;
};


export interface SystemInfo {
export type SystemInfo = {
startupTime: Date;
os_version: string;
bot_version: string;
Expand All @@ -106,7 +124,7 @@ export interface SystemInfo {
cpu: string;
}

export interface SystemStatus {
export type SystemStatus = {
load: {
percent: string;
detail: string;
Expand Down
6 changes: 3 additions & 3 deletions src/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class Controller {
this.#shardManager = new ShardingController();

this.#localNodeController = new LocalNodeController(
this.#bot.config.localNode.downloadLink,
this.#bot.config.localNode.downloadLink!,
this.#bot.logger,
this.#bot.config.localNode.autoRestart
);
Expand All @@ -44,9 +44,9 @@ export class Controller {
this.#bot.sysInfo = await getSysInfo();

return Promise.resolve()
.then(async () => { if (this.#bot.config.enableLocalNode) await loadLocalNode(this.#bot, this.#localNodeController); })
.then(async () => { if (this.#bot.config.localNode.enabled) await loadLocalNode(this.#bot, this.#localNodeController); })
.then(() => this.#shardManager.spwan())
.then(async () => { if (this.#bot.config.enableSite) await loadSite(this.#bot, this.#shardManager.manager, this.#localNodeController); })
.then(async () => { if (this.#bot.config.webDashboard.enabled) await loadSite(this.#bot, this.#shardManager.manager, this.#localNodeController); })
.then(() => {
this.#bot.logger.emit('shard', cst.color.green + '*** ShardingController initialization completed ***' + cst.color.white);
this.#bot.logger.emit('shard', 'Launching sharding process...');
Expand Down
7 changes: 4 additions & 3 deletions src/ShardingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ export class ShardingController {
public manager: ShardingManager;

constructor() {
dotenv.config();

const fileExtension = path.extname(__filename);
this.shardFilePath = path.join(__dirname, `./App${fileExtension}`);

this.manager = new ShardingManager(this.shardFilePath, { token: process.env.BOT_TOKEN });
}


public spwan() {
dotenv.config();
this.manager = new ShardingManager(this.shardFilePath, { token: process.env.BOT_TOKEN });

this.manager.spawn();
}
}
4 changes: 2 additions & 2 deletions src/commands/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const execute = async (bot: Bot, client: Client, message: Message, args:
i.deferUpdate();
await msg.edit({
content: '',
embeds: [embeds.filterMsg(bot.config.embedsColor, effectName)],
embeds: [embeds.filterMsg(bot.config.bot.embedsColor, effectName)],
components: [],
allowedMentions: { repliedUser: false }
})
Expand Down Expand Up @@ -158,7 +158,7 @@ export const slashExecute = async (bot: Bot, client: Client, interaction: ChatIn

return interaction.editReply({
content: '',
embeds: [embeds.filterMsg(bot.config.embedsColor, effectName ?? 'unknown')],
embeds: [embeds.filterMsg(bot.config.bot.embedsColor, effectName ?? 'unknown')],
components: [],
allowedMentions: { repliedUser: false }
})
Expand Down
12 changes: 6 additions & 6 deletions src/commands/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const options = [


export const execute = async (bot: Bot, client: Client, message: Message, args: string[]) => {
const prefix = bot.config.prefix;
const prefix = bot.config.bot.prefix;

if (!args[0]) {
const title = client.user?.username;
Expand Down Expand Up @@ -68,7 +68,7 @@ export const execute = async (bot: Bot, client: Client, message: Message, args:

i.deferUpdate();
await msg.edit({
embeds: [embeds.help(bot.config.embedsColor, title!, usage)],
embeds: [embeds.help(bot.config.bot.embedsColor, title!, usage)],
components: [],
allowedMentions: { repliedUser: false }
})
Expand Down Expand Up @@ -99,7 +99,7 @@ export const execute = async (bot: Bot, client: Client, message: Message, args:
const description = `${x.description}\n\`\`\`${prefix}${x.usage}\`\`\``;

message.reply({
embeds: [embeds.help(bot.config.embedsColor, command, description)],
embeds: [embeds.help(bot.config.bot.embedsColor, command, description)],
allowedMentions: { repliedUser: false }
});
return true;
Expand All @@ -111,7 +111,7 @@ export const execute = async (bot: Bot, client: Client, message: Message, args:
};

export const slashExecute = async (bot: Bot, client: Client, interaction: ChatInputCommandInteraction) => {
const prefix = bot.config.prefix;
const prefix = bot.config.bot.prefix;
const command = interaction.options.getString("command");

if (!command) {
Expand Down Expand Up @@ -148,7 +148,7 @@ export const slashExecute = async (bot: Bot, client: Client, interaction: ChatIn

i.deferUpdate();
await msg.edit({
embeds: [embeds.help(bot.config.embedsColor, title!, usage)],
embeds: [embeds.help(bot.config.bot.embedsColor, title!, usage)],
components: [],
allowedMentions: { repliedUser: false }
})
Expand Down Expand Up @@ -179,7 +179,7 @@ export const slashExecute = async (bot: Bot, client: Client, interaction: ChatIn
const description = `${x.description}\n\`\`\`${prefix}${x.usage}\`\`\``;

interaction.editReply({
embeds: [embeds.help(bot.config.embedsColor, command, description)],
embeds: [embeds.help(bot.config.bot.embedsColor, command, description)],
allowedMentions: { repliedUser: false }
});
return true;
Expand Down
8 changes: 4 additions & 4 deletions src/commands/leave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ export const execute = async (bot: Bot, client: Client, message: Message) => {
return message.reply({ content: '❌ | There is no music currently playing.', allowedMentions: { repliedUser: false } });
}

if (bot.config.autoLeave) {
if (bot.config.bot.autoLeave.enabled) {
player.destroy();
}
else {
player.queue.clear();
await player.skip();
await dashboard.destroy(bot, player, bot.config.embedsColor);
await dashboard.destroy(bot, player, bot.config.bot.embedsColor);
}

return message.react('👍');
Expand All @@ -41,13 +41,13 @@ export const slashExecute = async (bot: Bot, client: Client, interaction: ChatIn
return interaction.editReply({ content: '❌ | There is no music currently playing.', allowedMentions: { repliedUser: false } });
}

if (bot.config.autoLeave) {
if (bot.config.bot.autoLeave.enabled) {
player.destroy();
}
else {
player.queue.clear();
await player.skip();
await dashboard.destroy(bot, player, bot.config.embedsColor);
await dashboard.destroy(bot, player, bot.config.bot.embedsColor);
}

return interaction.editReply('✅ | Bot leave.');
Expand Down
Loading

0 comments on commit 69ffe87

Please sign in to comment.