Skip to content

Instantly share code, notes, and snippets.

@cakedan
Last active March 17, 2020 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cakedan/473a777332cee28265649efce6d41e69 to your computer and use it in GitHub Desktop.
Save cakedan/473a777332cee28265649efce6d41e69 to your computer and use it in GitHub Desktop.
const { CommandClient, Constants } = require('detritus-client');
const { Permissions } = Constants;
const commandClient = new CommandClient('', {
gateway: {loadAllMembers: true},
prefix: 'fat',
});
const permissionsClient = [
Permissions.MANAGE_CHANNELS,
Permissions.MANAGE_GUILD,
Permissions.MANAGE_WEBHOOKS,
];
const onPermissionsFailClient = (context) => context.reply('I need Manage Channels, Manage Guild, and Manage Webhooks');
commandClient.add({
name: 'ping',
run: (context) => context.editOrReply('ping'),
});
commandClient.add({
name: 'create',
permissionsClient,
onPermissionsFailClient,
onBefore: (context) => context.client.isOwner(context.userId),
onCancel: (context) => context.reply('no'),
run: async (context) => {
const category = await context.guild.createChannel({name: 'ping', type: 4});
for (let i = 0; i < 50; i++) {
const channel = await context.guild.createChannel({name: `ping-${i + 1}`, parentId: category.id, type: 0});
const webhook = await channel.createWebhook({name: channel.id});
}
return context.editOrReply(`ok, created 50 ping channels and 1 category`);
},
});
commandClient.add({
name: 'delete',
permissionsClient,
onPermissionsFailClient,
onBefore: (context) => context.client.isOwner(context.userId),
onCancel: (context) => context.reply('no'),
run: async (context) => {
let amount = 0;
const categories = context.guild.channels.filter((channel) => channel.isGuildCategory && channel.name === 'ping');
for (let category of categories) {
for (let [channelId, channel] of category.children) {
await channel.delete();
amount++;
}
await category.delete();
amount++;
}
return context.editOrReply(`ok, deleted ${amount} channels`);
},
});
const MAX_ERRORS = 10;
const CHUNK_AMOUNT = 5;
const doing = new Set();
commandClient.add({
name: 'start',
permissionsClient,
onPermissionsFailClient,
onBefore: (context) => context.client.isOwner(context.userId),
onCancel: (context) => context.reply('no'),
run: async (context) => {
if (doing.has(context.guildId)) {
return context.editOrReply('Already Started');
}
doing.add(context.guildId);
await context.editOrReply('ok starting');
const owner = context.guild.owner;
const chunks = [];
const webhooks = (await context.guild.fetchWebhooks()).filter((webhook) => {
return webhook.name === webhook.channelId;
});
for (let i = 0; i < webhooks.length; i += CHUNK_AMOUNT) {
chunks.push(webhooks.slice(i, i + CHUNK_AMOUNT));
}
const errors = [];
while (true) {
for (const chunk of chunks) {
if (!doing.has(context.guildId)) {
return context.editOrReply('stopped');
}
try {
const promises = chunk.map((webhook) => {
return webhook.createMessage({
avatarUrl: owner.avatarUrl,
content: `@everyone ${context.guild.members.map((member) => member.mention).join('')}`.slice(0, 2000),
username: owner.name,
});
});
await Promise.all(promises);
} catch (error) {
if (error.response) {
switch (error.response.statusCode) {
case 500:
case 502: {
errors.push(error);
}; break;
default: {
throw error;
};
}
} else {
throw error;
}
if (MAX_ERRORS < errors.length) {
return context.editOrReply(`Reached ${errors.length} 500 errors.`);
}
}
}
}
},
onRunError: (context, args, error) => {
doing.delete(context.guildId);
return context.editOrReply(`${error}`);
},
});
commandClient.add({
name: 'stop',
permissionsClient,
onPermissionsFailClient,
onBefore: (context) => context.client.isOwner(context.userId),
onCancel: (context) => context.reply('no'),
run: async (context) => {
if (doing.has(context.guildId)) {
doing.delete(context.guildId);
return context.editOrReply('ok stopped');
}
return context.editOrReply('ok ill stop, but i never started');
},
});
(async () => {
await commandClient.run();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment