Kyanq
Kyanq
DIAdiscord.js - Imagine an app
Created by Kyanq on 7/17/2023 in #djs-questions
Dublicate shard. Many client instances for one shard.
ok, thanks
13 replies
DIAdiscord.js - Imagine an app
Created by Kyanq on 7/17/2023 in #djs-questions
Dublicate shard. Many client instances for one shard.
I can see the structure close to the ideal somewhere?
13 replies
DIAdiscord.js - Imagine an app
Created by Kyanq on 7/17/2023 in #djs-questions
Dublicate shard. Many client instances for one shard.
thanks for the advice, and what template would you recommend, what should I strive for?
13 replies
DIAdiscord.js - Imagine an app
Created by Kyanq on 7/17/2023 in #djs-questions
Dublicate shard. Many client instances for one shard.
I removed it, but it still launches repeatedly. Writes Shard Launched 0, Shard Launched 1 several times and restarts each time. Therefore, the bot responds to one command many times in the same shard
13 replies
DIAdiscord.js - Imagine an app
Created by Kyanq on 7/17/2023 in #djs-questions
Dublicate shard. Many client instances for one shard.
do you mean this?
client.login(process.env.TOKEN)
.then(async () => {
await Log.init(client);
require(`./handlers/events.js`).init(client);
require(`./handlers/commands.js`).init(client);
});
client.login(process.env.TOKEN)
.then(async () => {
await Log.init(client);
require(`./handlers/events.js`).init(client);
require(`./handlers/commands.js`).init(client);
});
13 replies
DIAdiscord.js - Imagine an app
Created by Kyanq on 7/17/2023 in #djs-questions
Dublicate shard. Many client instances for one shard.
yes, I just created bot.js, transferred the contents of index.js there, and wrote the code for creating shards in index.js. And I tried to fix the problem with creating multiple clients for one shard in commands.js
13 replies
DIAdiscord.js - Imagine an app
Created by Kyanq on 7/17/2023 in #djs-questions
Dublicate shard. Many client instances for one shard.
ok, sorry handlers/commands.js
const {lstat, readdir} = require('fs/promises'),
{join} = require('path'),
BaseCommand = require('../structures/BaseCommand');

module.exports.init = async (client) => {
Log.send(`[HANDLER/EVENTS] Хандлер Slash-комманд запущен.`);
const slashes = await walk(client, './commands/').catch(console.error);
client.application.commands.set(slashes)
.then(() => {
Log.send(`[HANDLER/COMMANDS] Установлено ${slashes.length} глобальных slash-комманд.`);
})
.catch((e) => {
Log.error(`[HANDLER/COMMANDS] Ошибка установки глобальных slash-комманд: ${e}`);
});
Log.send(`[HANDLER/COMMANDS] Загружено ${client.commands.size} комманд.`);
}

async function walk(client, dir, slashes = []) {
const files = await readdir(dir);

for (const file of files) {
const filePath = join(dir, file);
const fileStat = await lstat(filePath);

if (fileStat.isDirectory()) {
await walk(client, filePath, slashes);
} else {
const cmdClass = require(`../${filePath}`);
const cmd = new cmdClass();

if (cmd instanceof BaseCommand && !client.commands.has(cmd.name)) {
client.commands.set(cmd.name, cmd);

if (cmd.type.includes(Config.CommandType.SLASH_APPLICATION)) {
slashes.push(cmd.slash.toJSON());
}
}
}
}

return slashes;
}
const {lstat, readdir} = require('fs/promises'),
{join} = require('path'),
BaseCommand = require('../structures/BaseCommand');

module.exports.init = async (client) => {
Log.send(`[HANDLER/EVENTS] Хандлер Slash-комманд запущен.`);
const slashes = await walk(client, './commands/').catch(console.error);
client.application.commands.set(slashes)
.then(() => {
Log.send(`[HANDLER/COMMANDS] Установлено ${slashes.length} глобальных slash-комманд.`);
})
.catch((e) => {
Log.error(`[HANDLER/COMMANDS] Ошибка установки глобальных slash-комманд: ${e}`);
});
Log.send(`[HANDLER/COMMANDS] Загружено ${client.commands.size} комманд.`);
}

async function walk(client, dir, slashes = []) {
const files = await readdir(dir);

for (const file of files) {
const filePath = join(dir, file);
const fileStat = await lstat(filePath);

if (fileStat.isDirectory()) {
await walk(client, filePath, slashes);
} else {
const cmdClass = require(`../${filePath}`);
const cmd = new cmdClass();

if (cmd instanceof BaseCommand && !client.commands.has(cmd.name)) {
client.commands.set(cmd.name, cmd);

if (cmd.type.includes(Config.CommandType.SLASH_APPLICATION)) {
slashes.push(cmd.slash.toJSON());
}
}
}
}

return slashes;
}
structures/BaseCommand.js: https://github.com/iamnotacoder-djs/DiscordBotIda/blob/master/structures/BaseCommand.js everything else is the same as on that github i changed only these 3 files: index.js, bot.js, handlers/commands.js
13 replies
DIAdiscord.js - Imagine an app
Created by Kyanq on 7/17/2023 in #djs-questions
Dublicate shard. Many client instances for one shard.
index.js code:
const { ShardingManager } = require('discord.js');

const manager = new ShardingManager('./bot.js', {
token: process.env.TOKEN,
totalShards: 2,
mode: "worker"
});

manager.on('shardCreate', async (shard) => {
console.log(`Shard Launched ${shard.id}`)
shard.on('error', (error) => {
console.error(error)
})
});

manager.spawn().catch(e => console.log(e));
const { ShardingManager } = require('discord.js');

const manager = new ShardingManager('./bot.js', {
token: process.env.TOKEN,
totalShards: 2,
mode: "worker"
});

manager.on('shardCreate', async (shard) => {
console.log(`Shard Launched ${shard.id}`)
shard.on('error', (error) => {
console.error(error)
})
});

manager.spawn().catch(e => console.log(e));
bot.js code:
const {Client, Collection, IntentsBitField, GatewayIntentBits} = require("discord.js"),
client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildVoiceStates
]
})

require('dotenv').config()

global.Config = new ConfigUtil();
global.Log = new Logger();
client.commands = new Collection();

client.login(process.env.TOKEN)
.then(async () => {
await Log.init(client);
require(`./handlers/events.js`).init(client);

require(`./handlers/commands.js`).init(client);
});

client.on('error', Log.error)
client.on('warn', Log.error)
process.on('uncaughtException', Log.error);
process.on('unhandledRejection', Log.error);

module.exports = client;
const {Client, Collection, IntentsBitField, GatewayIntentBits} = require("discord.js"),
client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildVoiceStates
]
})

require('dotenv').config()

global.Config = new ConfigUtil();
global.Log = new Logger();
client.commands = new Collection();

client.login(process.env.TOKEN)
.then(async () => {
await Log.init(client);
require(`./handlers/events.js`).init(client);

require(`./handlers/commands.js`).init(client);
});

client.on('error', Log.error)
client.on('warn', Log.error)
process.on('uncaughtException', Log.error);
process.on('unhandledRejection', Log.error);

module.exports = client;
13 replies