mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-20 21:44:48 +02:00
Update SequelizeProvider and do some other stuff
This commit is contained in:
@@ -1 +1 @@
|
|||||||
worker: node shardingmanager.js
|
worker: node shardingmanager.js
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ const path = require('path');
|
|||||||
const { carbon, discordBots } = require('./structures/Stats');
|
const { carbon, discordBots } = require('./structures/Stats');
|
||||||
const SequelizeProvider = require('./providers/Sequelize');
|
const SequelizeProvider = require('./providers/Sequelize');
|
||||||
const Database = require('./structures/PostgreSQL');
|
const Database = require('./structures/PostgreSQL');
|
||||||
const database = new Database();
|
|
||||||
database.start();
|
Database.start();
|
||||||
|
|
||||||
client.setProvider(new SequelizeProvider(Database.db));
|
client.setProvider(new SequelizeProvider(Database.db));
|
||||||
|
|
||||||
@@ -74,13 +74,13 @@ client.on('guildCreate', async(guild) => {
|
|||||||
console.log(`[Count] ${count}`);
|
console.log(`[Count] ${count}`);
|
||||||
try {
|
try {
|
||||||
await carbon(count);
|
await carbon(count);
|
||||||
console.log(`[Carbon] Successfully posted to Carbon.`);
|
console.log('[Carbon] Successfully posted to Carbon.');
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
console.log(`[Carbon] Failed to post to Carbon. ${err}`);
|
console.log(`[Carbon] Failed to post to Carbon. ${err}`);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
await discordBots(count, client.user.id);
|
await discordBots(count, client.user.id);
|
||||||
console.log(`[Discord Bots] Successfully posted to Discord Bots.`);
|
console.log('[Discord Bots] Successfully posted to Discord Bots.');
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
console.log(`[Discord Bots] Failed to post to Discord Bots. ${err}`);
|
console.log(`[Discord Bots] Failed to post to Discord Bots. ${err}`);
|
||||||
}
|
}
|
||||||
@@ -93,13 +93,13 @@ client.on('guildDelete', async(guild) => {
|
|||||||
console.log(`[Count] ${count}`);
|
console.log(`[Count] ${count}`);
|
||||||
try {
|
try {
|
||||||
await carbon(count);
|
await carbon(count);
|
||||||
console.log(`[Carbon] Successfully posted to Carbon.`);
|
console.log('[Carbon] Successfully posted to Carbon.');
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
console.log(`[Carbon] Failed to post to Carbon. ${err}`);
|
console.log(`[Carbon] Failed to post to Carbon. ${err}`);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
await discordBots(count, client.user.id);
|
await discordBots(count, client.user.id);
|
||||||
console.log(`[Discord Bots] Successfully posted to Discord Bots.`);
|
console.log('[Discord Bots] Successfully posted to Discord Bots.');
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
console.log(`[Discord Bots] Failed to post to Discord Bots. ${err}`);
|
console.log(`[Discord Bots] Failed to post to Discord Bots. ${err}`);
|
||||||
}
|
}
|
||||||
@@ -120,6 +120,4 @@ client.on('ready', () => {
|
|||||||
client.user.setGame(`x;help | Shard ${client.shard.id}`);
|
client.user.setGame(`x;help | Shard ${client.shard.id}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on('unhandledRejection', console.error);
|
|
||||||
|
|
||||||
client.login(process.env.TOKEN);
|
client.login(process.env.TOKEN);
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "xiaobot",
|
"name": "xiaobot",
|
||||||
"version": "18.6.1",
|
"version": "18.6.2",
|
||||||
"description": "A Discord Bot",
|
"description": "A Discord Bot",
|
||||||
"main": "shardingmanager.js",
|
"main": "shardingmanager.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
+40
-34
@@ -12,20 +12,20 @@ class SequelizeProvider extends SettingProvider {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {PostgreSQLDatabase} db - Database for the provider
|
* @param {SQLDatabase} db - Database for the provider
|
||||||
*/
|
*/
|
||||||
constructor(db) {
|
constructor(db) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Database that will be used for storing/retrieving settings
|
* Database that will be used for storing/retrieving settings
|
||||||
* @type {PostgreSQLDatabase}
|
* @type {SQLDatabase}
|
||||||
*/
|
*/
|
||||||
this.db = db;
|
this.db = db;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Client that the provider is for (set once the client is ready, after using {@link CommandoClient#setProvider})
|
* Client that the provider is for (set once the client is ready, after using {@link CommandoClient#setProvider})
|
||||||
* @name PostgreSQLProvider#client
|
* @name SequelizeProvider#client
|
||||||
* @type {CommandoClient}
|
* @type {CommandoClient}
|
||||||
* @readonly
|
* @readonly
|
||||||
*/
|
*/
|
||||||
@@ -57,7 +57,7 @@ class SequelizeProvider extends SettingProvider {
|
|||||||
unique: true,
|
unique: true,
|
||||||
primaryKey: true
|
primaryKey: true
|
||||||
},
|
},
|
||||||
settings: { type: Sequelize.STRING }
|
settings: { type: Sequelize.TEXT }
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -72,19 +72,19 @@ class SequelizeProvider extends SettingProvider {
|
|||||||
|
|
||||||
// Load all settings
|
// Load all settings
|
||||||
const rows = await this.model.findAll();
|
const rows = await this.model.findAll();
|
||||||
for(const row of rows) {
|
for (const row of rows) {
|
||||||
let settings;
|
let settings;
|
||||||
try {
|
try {
|
||||||
settings = JSON.parse(row.dataValues.settings);
|
settings = JSON.parse(row.dataValues.settings);
|
||||||
} catch(err) {
|
} catch (err) {
|
||||||
client.emit('warn', `PostgreSQLProvider couldn't parse the settings stored for guild ${row.dataValues.guild}.`);
|
client.emit('warn', `SequelizeProvider couldn't parse the settings stored for guild ${row.dataValues.guild}.`);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const guild = row.dataValues.guild !== '0' ? row.dataValues.guild : 'global';
|
const guild = row.dataValues.guild !== '0' ? row.dataValues.guild : 'global';
|
||||||
|
|
||||||
this.settings.set(guild, settings);
|
this.settings.set(guild, settings);
|
||||||
if(guild !== 'global' && !client.guilds.has(row.dataValues.guild)) continue;
|
if (guild !== 'global' && !client.guilds.has(row.dataValues.guild)) continue;
|
||||||
this.setupGuild(guild, settings);
|
this.setupGuild(guild, settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,27 +95,27 @@ class SequelizeProvider extends SettingProvider {
|
|||||||
.set('groupStatusChange', (guild, group, enabled) => this.set(guild, `grp-${group.id}`, enabled))
|
.set('groupStatusChange', (guild, group, enabled) => this.set(guild, `grp-${group.id}`, enabled))
|
||||||
.set('guildCreate', guild => {
|
.set('guildCreate', guild => {
|
||||||
const settings = this.settings.get(guild.id);
|
const settings = this.settings.get(guild.id);
|
||||||
if(!settings) return;
|
if (!settings) return;
|
||||||
this.setupGuild(guild.id, settings);
|
this.setupGuild(guild.id, settings);
|
||||||
})
|
})
|
||||||
.set('commandRegister', command => {
|
.set('commandRegister', command => {
|
||||||
for(const [guild, settings] of this.settings) {
|
for (const [guild, settings] of this.settings) {
|
||||||
if(guild !== 'global' && !client.guilds.has(guild)) continue;
|
if (guild !== 'global' && !client.guilds.has(guild)) continue;
|
||||||
this.setupGuildCommand(client.guilds.get(guild), command, settings);
|
this.setupGuildCommand(client.guilds.get(guild), command, settings);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.set('groupRegister', group => {
|
.set('groupRegister', group => {
|
||||||
for(const [guild, settings] of this.settings) {
|
for (const [guild, settings] of this.settings) {
|
||||||
if(guild !== 'global' && !client.guilds.has(guild)) continue;
|
if (guild !== 'global' && !client.guilds.has(guild)) continue;
|
||||||
this.setupGuildGroup(client.guilds.get(guild), group, settings);
|
this.setupGuildGroup(client.guilds.get(guild), group, settings);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
for(const [event, listener] of this.listeners) client.on(event, listener);
|
for (const [event, listener] of this.listeners) client.on(event, listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy() {
|
destroy() {
|
||||||
// Remove all listeners from the client
|
// Remove all listeners from the client
|
||||||
for(const [event, listener] of this.listeners) this.client.removeListener(event, listener);
|
for (const [event, listener] of this.listeners) this.client.removeListener(event, listener);
|
||||||
this.listeners.clear();
|
this.listeners.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,32 +127,38 @@ class SequelizeProvider extends SettingProvider {
|
|||||||
async set(guild, key, val) {
|
async set(guild, key, val) {
|
||||||
guild = this.constructor.getGuildID(guild);
|
guild = this.constructor.getGuildID(guild);
|
||||||
let settings = this.settings.get(guild);
|
let settings = this.settings.get(guild);
|
||||||
if(!settings) {
|
if (!settings) {
|
||||||
settings = {};
|
settings = {};
|
||||||
this.settings.set(guild, settings);
|
this.settings.set(guild, settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
settings[key] = val;
|
settings[key] = val;
|
||||||
await this.model.upsert({ guild: guild !== 'global' ? guild : '0', settings: JSON.stringify(settings) }, { where: { guild: guild !== 'global' ? guild : '0' } }); // eslint-disable-line max-len
|
await this.model.upsert(
|
||||||
if(guild === 'global') this.updateOtherShards(key, val);
|
{ guild: guild !== 'global' ? guild : '0', settings: JSON.stringify(settings) },
|
||||||
|
{ where: { guild: guild !== 'global' ? guild : '0' } }
|
||||||
|
);
|
||||||
|
if (guild === 'global') this.updateOtherShards(key, val);
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
async remove(guild, key) {
|
async remove(guild, key) {
|
||||||
guild = this.constructor.getGuildID(guild);
|
guild = this.constructor.getGuildID(guild);
|
||||||
const settings = this.settings.get(guild);
|
const settings = this.settings.get(guild);
|
||||||
if(!settings || typeof settings[key] === 'undefined') return undefined;
|
if (!settings || typeof settings[key] === 'undefined') return undefined;
|
||||||
|
|
||||||
const val = settings[key];
|
const val = settings[key];
|
||||||
settings[key] = undefined;
|
settings[key] = undefined;
|
||||||
await this.model.upsert({ guild: guild !== 'global' ? guild : '0', settings: JSON.stringify(settings) }, { where: { guild: guild !== 'global' ? guild : '0' } }); // eslint-disable-line max-len
|
await this.model.upsert(
|
||||||
if(guild === 'global') this.updateOtherShards(key, undefined);
|
{ guild: guild !== 'global' ? guild : '0', settings: JSON.stringify(settings) },
|
||||||
|
{ where: { guild: guild !== 'global' ? guild : '0' } }
|
||||||
|
);
|
||||||
|
if (guild === 'global') this.updateOtherShards(key, undefined);
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
async clear(guild) {
|
async clear(guild) {
|
||||||
guild = this.constructor.getGuildID(guild);
|
guild = this.constructor.getGuildID(guild);
|
||||||
if(!this.settings.has(guild)) return;
|
if (!this.settings.has(guild)) return;
|
||||||
this.settings.delete(guild);
|
this.settings.delete(guild);
|
||||||
await this.model.destroy({ where: { guild: guild !== 'global' ? guild : '0' } });
|
await this.model.destroy({ where: { guild: guild !== 'global' ? guild : '0' } });
|
||||||
}
|
}
|
||||||
@@ -164,18 +170,18 @@ class SequelizeProvider extends SettingProvider {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
setupGuild(guild, settings) {
|
setupGuild(guild, settings) {
|
||||||
if(typeof guild !== 'string') throw new TypeError('The guild must be a guild ID or "global".');
|
if (typeof guild !== 'string') throw new TypeError('The guild must be a guild ID or "global".');
|
||||||
guild = this.client.guilds.get(guild) || null;
|
guild = this.client.guilds.get(guild) || null;
|
||||||
|
|
||||||
// Load the command prefix
|
// Load the command prefix
|
||||||
if(typeof settings.prefix !== 'undefined') {
|
if (typeof settings.prefix !== 'undefined') {
|
||||||
if(guild) guild._commandPrefix = settings.prefix;
|
if (guild) guild._commandPrefix = settings.prefix;
|
||||||
else this.client._commandPrefix = settings.prefix;
|
else this.client._commandPrefix = settings.prefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load all command/group statuses
|
// Load all command/group statuses
|
||||||
for(const command of this.client.registry.commands.values()) this.setupGuildCommand(guild, command, settings);
|
for (const command of this.client.registry.commands.values()) this.setupGuildCommand(guild, command, settings);
|
||||||
for(const group of this.client.registry.groups.values()) this.setupGuildGroup(guild, group, settings);
|
for (const group of this.client.registry.groups.values()) this.setupGuildGroup(guild, group, settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -186,9 +192,9 @@ class SequelizeProvider extends SettingProvider {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
setupGuildCommand(guild, command, settings) {
|
setupGuildCommand(guild, command, settings) {
|
||||||
if(typeof settings[`cmd-${command.name}`] === 'undefined') return;
|
if (typeof settings[`cmd-${command.name}`] === 'undefined') return;
|
||||||
if(guild) {
|
if (guild) {
|
||||||
if(!guild._commandsEnabled) guild._commandsEnabled = {};
|
if (!guild._commandsEnabled) guild._commandsEnabled = {};
|
||||||
guild._commandsEnabled[command.name] = settings[`cmd-${command.name}`];
|
guild._commandsEnabled[command.name] = settings[`cmd-${command.name}`];
|
||||||
} else {
|
} else {
|
||||||
command._globalEnabled = settings[`cmd-${command.name}`];
|
command._globalEnabled = settings[`cmd-${command.name}`];
|
||||||
@@ -203,9 +209,9 @@ class SequelizeProvider extends SettingProvider {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
setupGuildGroup(guild, group, settings) {
|
setupGuildGroup(guild, group, settings) {
|
||||||
if(typeof settings[`grp-${group.id}`] === 'undefined') return;
|
if (typeof settings[`grp-${group.id}`] === 'undefined') return;
|
||||||
if(guild) {
|
if (guild) {
|
||||||
if(!guild._groupsEnabled) guild._groupsEnabled = {};
|
if (!guild._groupsEnabled) guild._groupsEnabled = {};
|
||||||
guild._groupsEnabled[group.id] = settings[`grp-${group.id}`];
|
guild._groupsEnabled[group.id] = settings[`grp-${group.id}`];
|
||||||
} else {
|
} else {
|
||||||
group._globalEnabled = settings[`grp-${group.id}`];
|
group._globalEnabled = settings[`grp-${group.id}`];
|
||||||
@@ -219,7 +225,7 @@ class SequelizeProvider extends SettingProvider {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
updateOtherShards(key, val) {
|
updateOtherShards(key, val) {
|
||||||
if(!this.client.shard) return;
|
if (!this.client.shard) return;
|
||||||
key = JSON.stringify(key);
|
key = JSON.stringify(key);
|
||||||
val = typeof val !== 'undefined' ? JSON.stringify(val) : 'undefined';
|
val = typeof val !== 'undefined' ? JSON.stringify(val) : 'undefined';
|
||||||
this.client.shard.broadcastEval(`
|
this.client.shard.broadcastEval(`
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
const { ShardingManager } = require('discord.js');
|
const { ShardingManager } = require('discord.js');
|
||||||
const Manager = new ShardingManager('./index.js', { token: process.env.TOKEN });
|
const Manager = new ShardingManager('./index.js', { token: process.env.TOKEN });
|
||||||
Manager.spawn();
|
Manager.spawn();
|
||||||
|
|
||||||
process.on('unhandledRejection', console.error);
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ class Database {
|
|||||||
return database;
|
return database;
|
||||||
}
|
}
|
||||||
|
|
||||||
start() {
|
static start() {
|
||||||
database.authenticate()
|
database.authenticate()
|
||||||
.then(() => console.log('[Database] Connection has been established successfully.'))
|
.then(() => console.log('[Database] Connection has been established successfully.'))
|
||||||
.then(() => console.log('[Database] Synchronizing...'))
|
.then(() => console.log('[Database] Synchronizing...'))
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
const request = require('superagent');
|
const request = require('superagent');
|
||||||
|
|
||||||
class Stats {
|
class Stats {
|
||||||
constructor() {
|
|
||||||
throw new Error(`The ${this.constructor.name} class may not be instantiated.`);
|
|
||||||
}
|
|
||||||
|
|
||||||
static discordBots(server_count, userID) {
|
static discordBots(server_count, userID) {
|
||||||
return request
|
return request
|
||||||
.post(`https://bots.discord.pw/api/bots/${userID}/stats`)
|
.post(`https://bots.discord.pw/api/bots/${userID}/stats`)
|
||||||
|
|||||||
Reference in New Issue
Block a user