chore: monorepo setup (#7175)

This commit is contained in:
Noel
2022-01-07 17:18:25 +01:00
committed by GitHub
parent 780b7ed39f
commit 16390efe6e
504 changed files with 25459 additions and 22830 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

View File

@@ -0,0 +1,32 @@
'use strict';
const assert = require('node:assert');
const { token } = require('./auth');
const { Client, Intents } = require('../src');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
client.on('ready', async () => {
try {
const guild = await client.guilds.create('testing', {
channels: [
{ name: 'afk channel', type: 'GUILD_VOICE', id: 0 },
{ name: 'system-channel', id: 1 },
],
afkChannelId: 0,
afkTimeout: 60,
systemChannelId: 1,
});
console.log(guild.id);
assert.strictEqual(guild.afkChannel.name, 'afk channel');
assert.strictEqual(guild.afkTimeout, 60);
assert.strictEqual(guild.systemChannel.name, 'system-channel');
await guild.delete();
} catch (error) {
console.error(error);
} finally {
client.destroy();
}
});
client.login(token).catch(console.error);

View File

@@ -0,0 +1,196 @@
'use strict';
/* eslint-env jest */
/* eslint-disable max-len */
const Util = require('../src/util/Util');
const testString = "`_Behold!_`\n||___~~***```js\n`use strict`;\nrequire('discord.js');```***~~___||";
describe('escapeCodeblock', () => {
test('shared', () => {
expect(Util.escapeCodeBlock(testString)).toBe(
"`_Behold!_`\n||___~~***\\`\\`\\`js\n`use strict`;\nrequire('discord.js');\\`\\`\\`***~~___||",
);
});
test('basic', () => {
expect(Util.escapeCodeBlock('```test```')).toBe('\\`\\`\\`test\\`\\`\\`');
});
});
describe('escapeInlineCode', () => {
test('shared', () => {
expect(Util.escapeInlineCode(testString)).toBe(
"\\`_Behold!_\\`\n||___~~***```js\n\\`use strict\\`;\nrequire('discord.js');```***~~___||",
);
});
test('basic', () => {
expect(Util.escapeInlineCode('`test`')).toBe('\\`test\\`');
});
});
describe('escapeBold', () => {
test('shared', () => {
expect(Util.escapeBold(testString)).toBe(
"`_Behold!_`\n||___~~*\\*\\*```js\n`use strict`;\nrequire('discord.js');```\\*\\**~~___||",
);
});
test('basic', () => {
expect(Util.escapeBold('**test**')).toBe('\\*\\*test\\*\\*');
});
});
describe('escapeItalic', () => {
test('shared', () => {
expect(Util.escapeItalic(testString)).toBe(
"`\\_Behold!\\_`\n||\\___~~\\***```js\n`use strict`;\nrequire('discord.js');```**\\*~~__\\_||",
);
});
test('basic (_)', () => {
expect(Util.escapeItalic('_test_')).toBe('\\_test\\_');
});
test('basic (*)', () => {
expect(Util.escapeItalic('*test*')).toBe('\\*test\\*');
});
});
describe('escapeUnderline', () => {
test('shared', () => {
expect(Util.escapeUnderline(testString)).toBe(
"`_Behold!_`\n||_\\_\\_~~***```js\n`use strict`;\nrequire('discord.js');```***~~\\_\\__||",
);
});
test('basic', () => {
expect(Util.escapeUnderline('__test__')).toBe('\\_\\_test\\_\\_');
});
});
describe('escapeStrikethrough', () => {
test('shared', () => {
expect(Util.escapeStrikethrough(testString)).toBe(
"`_Behold!_`\n||___\\~\\~***```js\n`use strict`;\nrequire('discord.js');```***\\~\\~___||",
);
});
test('basic', () => {
expect(Util.escapeStrikethrough('~~test~~')).toBe('\\~\\~test\\~\\~');
});
});
describe('escapeSpoiler', () => {
test('shared', () => {
expect(Util.escapeSpoiler(testString)).toBe(
"`_Behold!_`\n\\|\\|___~~***```js\n`use strict`;\nrequire('discord.js');```***~~___\\|\\|",
);
});
test('basic', () => {
expect(Util.escapeSpoiler('||test||')).toBe('\\|\\|test\\|\\|');
});
});
describe('escapeMarkdown', () => {
test('shared', () => {
expect(Util.escapeMarkdown(testString)).toBe(
"\\`\\_Behold!\\_\\`\n\\|\\|\\_\\_\\_\\~\\~\\*\\*\\*\\`\\`\\`js\n\\`use strict\\`;\nrequire('discord.js');\\`\\`\\`\\*\\*\\*\\~\\~\\_\\_\\_\\|\\|",
);
});
test('no codeBlock', () => {
expect(Util.escapeMarkdown(testString, { codeBlock: false })).toBe(
"\\`\\_Behold!\\_\\`\n\\|\\|\\_\\_\\_\\~\\~\\*\\*\\*```js\n\\`use strict\\`;\nrequire('discord.js');```\\*\\*\\*\\~\\~\\_\\_\\_\\|\\|",
);
});
test('no inlineCode', () => {
expect(Util.escapeMarkdown(testString, { inlineCode: false })).toBe(
"`\\_Behold!\\_`\n\\|\\|\\_\\_\\_\\~\\~\\*\\*\\*\\`\\`\\`js\n`use strict`;\nrequire('discord.js');\\`\\`\\`\\*\\*\\*\\~\\~\\_\\_\\_\\|\\|",
);
});
test('no bold', () => {
expect(Util.escapeMarkdown(testString, { bold: false })).toBe(
"\\`\\_Behold!\\_\\`\n\\|\\|\\_\\_\\_\\~\\~\\***\\`\\`\\`js\n\\`use strict\\`;\nrequire('discord.js');\\`\\`\\`**\\*\\~\\~\\_\\_\\_\\|\\|",
);
});
test('no italic', () => {
expect(Util.escapeMarkdown(testString, { italic: false })).toBe(
"\\`_Behold!_\\`\n\\|\\|_\\_\\_\\~\\~*\\*\\*\\`\\`\\`js\n\\`use strict\\`;\nrequire('discord.js');\\`\\`\\`\\*\\**\\~\\~\\_\\__\\|\\|",
);
});
test('no underline', () => {
expect(Util.escapeMarkdown(testString, { underline: false })).toBe(
"\\`\\_Behold!\\_\\`\n\\|\\|\\___\\~\\~\\*\\*\\*\\`\\`\\`js\n\\`use strict\\`;\nrequire('discord.js');\\`\\`\\`\\*\\*\\*\\~\\~__\\_\\|\\|",
);
});
test('no strikethrough', () => {
expect(Util.escapeMarkdown(testString, { strikethrough: false })).toBe(
"\\`\\_Behold!\\_\\`\n\\|\\|\\_\\_\\_~~\\*\\*\\*\\`\\`\\`js\n\\`use strict\\`;\nrequire('discord.js');\\`\\`\\`\\*\\*\\*~~\\_\\_\\_\\|\\|",
);
});
test('no spoiler', () => {
expect(Util.escapeMarkdown(testString, { spoiler: false })).toBe(
"\\`\\_Behold!\\_\\`\n||\\_\\_\\_\\~\\~\\*\\*\\*\\`\\`\\`js\n\\`use strict\\`;\nrequire('discord.js');\\`\\`\\`\\*\\*\\*\\~\\~\\_\\_\\_||",
);
});
describe('code content', () => {
test('no code block content', () => {
expect(Util.escapeMarkdown(testString, { codeBlockContent: false })).toBe(
"\\`\\_Behold!\\_\\`\n\\|\\|\\_\\_\\_\\~\\~\\*\\*\\*\\`\\`\\`js\n`use strict`;\nrequire('discord.js');\\`\\`\\`\\*\\*\\*\\~\\~\\_\\_\\_\\|\\|",
);
});
test('no inline code content', () => {
expect(Util.escapeMarkdown(testString, { inlineCodeContent: false })).toBe(
"\\`_Behold!_\\`\n\\|\\|\\_\\_\\_\\~\\~\\*\\*\\*\\`\\`\\`js\n\\`use strict\\`;\nrequire('discord.js');\\`\\`\\`\\*\\*\\*\\~\\~\\_\\_\\_\\|\\|",
);
});
test('neither inline code or code block content', () => {
expect(Util.escapeMarkdown(testString, { inlineCodeContent: false, codeBlockContent: false }))
// eslint-disable-next-line max-len
.toBe(
"\\`_Behold!_\\`\n\\|\\|\\_\\_\\_\\~\\~\\*\\*\\*\\`\\`\\`js\n`use strict`;\nrequire('discord.js');\\`\\`\\`\\*\\*\\*\\~\\~\\_\\_\\_\\|\\|",
);
});
test('neither code blocks or code block content', () => {
expect(Util.escapeMarkdown(testString, { codeBlock: false, codeBlockContent: false })).toBe(
"\\`\\_Behold!\\_\\`\n\\|\\|\\_\\_\\_\\~\\~\\*\\*\\*```js\n`use strict`;\nrequire('discord.js');```\\*\\*\\*\\~\\~\\_\\_\\_\\|\\|",
);
});
test('neither inline code or inline code content', () => {
expect(Util.escapeMarkdown(testString, { inlineCode: false, inlineCodeContent: false })).toBe(
"`_Behold!_`\n\\|\\|\\_\\_\\_\\~\\~\\*\\*\\*\\`\\`\\`js\n`use strict`;\nrequire('discord.js');\\`\\`\\`\\*\\*\\*\\~\\~\\_\\_\\_\\|\\|",
);
});
test('edge-case odd number of fenses with no code block content', () => {
expect(
Util.escapeMarkdown('**foo** ```**bar**``` **fizz** ``` **buzz**', {
codeBlock: false,
codeBlockContent: false,
}),
).toBe('\\*\\*foo\\*\\* ```**bar**``` \\*\\*fizz\\*\\* ``` \\*\\*buzz\\*\\*');
});
test('edge-case odd number of backticks with no inline code content', () => {
expect(
Util.escapeMarkdown('**foo** `**bar**` **fizz** ` **buzz**', { inlineCode: false, inlineCodeContent: false }),
).toBe('\\*\\*foo\\*\\* `**bar**` \\*\\*fizz\\*\\* ` \\*\\*buzz\\*\\*');
});
});
});
/* eslint-enable max-len, no-undef */

View File

@@ -0,0 +1,257 @@
/* eslint-disable */
'use strict';
const request = require('superagent');
const ytdl = require('ytdl-core');
const { token, song } = require('./auth.js');
const { Client, Intents } = require('../src');
console.time('magic');
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.GUILD_MEMBERS,
],
});
client
.login(token)
.then(() => console.log('logged in'))
.catch(console.error);
// Fetch all members in a new guild
client.on('guildCreate', guild =>
guild.members.fetch().catch(err => console.log(`Failed to fetch all members: ${err}\n${err.stack}`)),
);
// Fetch all members in a newly available guild
client.on('guildUpdate', (oldGuild, newGuild) =>
!oldGuild.available && newGuild.available
? guild.members.fetch().catch(err => console.log(`Failed to fetch all members: ${err}\n${err.stack}`))
: Promise.resolve(),
);
client.on('ready', async () => {
// Fetch all members for initially available guilds
try {
const promises = client.guilds.cache.map(guild => (guild.available ? guild.members.fetch() : Promise.resolve()));
await Promise.all(promises);
} catch (err) {
console.log(`Failed to fetch all members before ready! ${err}\n${err.stack}`);
}
console.log(`ready with ${client.users.cache.size} users`);
console.timeEnd('magic');
});
client.on('debug', console.log);
client.on('error', m => console.log('debug', new Error(m).stack));
client.on('reconnecting', m => console.log('reconnecting', m));
client.on('messageCreate', message => {
if (true) {
if (message.content === 'makechann') {
if (message.channel.guild) {
message.channel.guild.channels.create('hi', { type: 'GUILD_TEXT' }).then(console.log);
}
}
if (message.content === 'imma queue pls') {
let count = 0;
let ecount = 0;
for (let x = 0; x < 4_000; x++) {
message.channel
.send(`this is message ${x} of 3999`)
.then(m => {
count++;
console.log('reached', count, ecount);
})
.catch(e => {
console.error(e);
ecount++;
console.log('reached', count, ecount);
});
}
}
if (message.content === 'myperms?') {
message.channel.send(
`Your permissions are:\n${JSON.stringify(message.channel.permissionsFor(message.author).serialize(), null, 4)}`,
);
}
if (message.content === 'delchann') {
message.channel.delete().then(chan => console.log('selfDelChann', chan.name));
}
if (message.content.startsWith('setname')) {
message.channel.setName(message.content.substr(8));
}
if (message.content.startsWith('botname')) {
client.user.setUsername(message.content.substr(8));
}
if (message.content.startsWith('botavatar')) {
request.get('url').end((err, res) => {
client.user
.setAvatar(res.body)
.catch(console.error)
.then(user => message.channel.send('Done!'));
});
}
if (message.content.startsWith('gn')) {
message.guild
.setName(message.content.substr(3))
.then(guild => console.log('guild updated to', guild.name))
.catch(console.error);
}
if (message.content === 'leave') {
message.guild
.leave()
.then(guild => console.log('left guild', guild.name))
.catch(console.error);
}
if (message.content === 'stats') {
let m = '';
m += `I am aware of ${message.guild.channels.cache.size} channels\n`;
m += `I am aware of ${message.guild.members.cache.size} members\n`;
m += `I am aware of ${client.channels.cache.size} channels overall\n`;
m += `I am aware of ${client.guilds.cache.size} guilds overall\n`;
m += `I am aware of ${client.users.cache.size} users overall\n`;
message.channel
.send(m)
.then(msg => msg.edit('nah'))
.catch(console.error);
}
if (message.content === 'messageme!') {
message.author.send('oh, hi there!').catch(e => console.log(e.stack));
}
if (message.content === "don't dm me") {
message.author.deleteDM();
}
if (message.content.startsWith('kick')) {
message.guild.members
.resolve(message.mentions.users.first())
.kick()
.then(member => {
console.log(member);
message.channel.send(`Kicked!${member.user.username}`);
})
.catch(console.error);
}
if (message.content === 'ratelimittest') {
let i = 1;
const start = Date.now();
while (i <= 20) {
message.channel.send(`Testing my rates, item ${i} of 20`);
i++;
}
message.channel.send('last one...').then(m => {
const diff = Date.now() - start;
m.channel.send(`Each message took ${diff / 21}ms to send`);
});
}
if (message.content === 'makerole') {
message.guild.roles
.create()
.then(role => {
message.channel.send(`Made role ${role.name}`);
})
.catch(console.error);
}
}
});
function nameLoop(user) {
// user.setUsername(user.username + 'a').then(nameLoop).catch(console.error);
}
function chanLoop(channel) {
channel.setName(`${channel.name}a`).then(chanLoop).catch(console.error);
}
client.on('messageCreate', msg => {
if (msg.content.startsWith('?raw')) {
msg.channel.send(`\`\`\`${msg.content}\`\`\``);
}
if (msg.content.startsWith('#eval') && msg.author.id === '66564597481480192') {
try {
const com = eval(msg.content.split(' ').slice(1).join(' '));
msg.channel.send(`\`\`\`\n${com}\`\`\``);
} catch (e) {
msg.channel.send(`\`\`\`\n${e}\`\`\``);
}
}
});
let disp, con;
client.on('messageCreate', msg => {
if (msg.content.startsWith('/play')) {
console.log('I am now going to play', msg.content);
const chan = msg.content.split(' ').slice(1).join(' ');
const s = ytdl(chan, { filter: 'audioonly' }, { passes: 3 });
s.on('error', e => console.log(`e w stream 1 ${e}`));
con.play(s);
}
if (msg.content.startsWith('/join')) {
const chan = msg.content.split(' ').slice(1).join(' ');
msg.channel.guild.channels.cache
.get(chan)
.join()
.then(conn => {
con = conn;
msg.channel.send('done');
const s = ytdl(song, { filter: 'audioonly' }, { passes: 3 });
s.on('error', e => console.log(`e w stream 2 ${e}`));
disp = conn.playStream(s);
conn.player.on('debug', console.log);
conn.player.on('error', err => console.log(123, err));
})
.catch(console.error);
}
});
client.on('messageReactionAdd', (reaction, user) => {
if (reaction.message.channelId !== '222086648706498562') return;
reaction.message.channel.send(`${user.username} added reaction ${reaction.emoji}, count is now ${reaction.count}`);
});
client.on('messageReactionRemove', (reaction, user) => {
if (reaction.message.channelId !== '222086648706498562') return;
reaction.message.channel.send(`${user.username} removed reaction ${reaction.emoji}, count is now ${reaction.count}`);
});
client.on('messageCreate', m => {
if (m.content.startsWith('#reactions')) {
const mId = m.content.split(' ')[1];
m.channel.messages.fetch(mId).then(rM => {
for (const reaction of rM.reactions.cache.values()) {
reaction.users.fetch().then(users => {
m.channel.send(
`The following gave that message ${reaction.emoji}:\n` +
`${users
.map(u => u.username)
.map(t => `- ${t}`)
.join('\n')}`,
);
});
}
});
}
});

View File

@@ -0,0 +1,39 @@
'use strict';
const { token, guildId, channelId, messageId } = require('./auth.js');
const { Client, Intents, ReactionCollector } = require('../src');
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS],
});
client.on('ready', async () => {
const guild = client.guilds.cache.get(guildId);
const channel = guild.channels.cache.get(channelId);
const message = await channel.messages.fetch(messageId);
await message.react('🔔');
// Await message.reactions.removeAll();
const collector = new ReactionCollector(message, () => true, { dispose: true });
collector.on('collect', () => {
console.log('collected');
});
collector.on('create', () => {
console.log('created');
});
collector.on('remove', () => {
console.log('removed');
});
collector.on('dispose', () => {
console.log('disposed');
});
});
client.login(token);

View File

@@ -0,0 +1,11 @@
'use strict';
/* eslint-env jest */
const { DataResolver } = require('../src');
describe('resolveGuildTemplateCode', () => {
test('basic', () => {
expect(DataResolver.resolveGuildTemplateCode('https://discord.new/abc')).toBe('abc');
});
});

View File

@@ -0,0 +1,116 @@
'use strict';
const fs = require('node:fs');
const path = require('node:path');
const process = require('node:process');
const { setTimeout: sleep } = require('node:timers/promises');
const util = require('node:util');
const fetch = require('node-fetch');
const { owner, token } = require('./auth.js');
const { Client, Intents, MessageAttachment, MessageEmbed } = require('../src');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const buffer = l => fetch(l).then(res => res.buffer());
const read = util.promisify(fs.readFile);
const readStream = fs.createReadStream;
const linkA = 'https://lolisafe.moe/iiDMtAXA.png';
const linkB = 'https://lolisafe.moe/9hSpedPh.png';
const fileA = path.join(__dirname, 'blobReach.png');
const embed = () => new MessageEmbed();
const attach = (attachment, name) => new MessageAttachment(attachment, name);
const tests = [
m => m.channel.send('x'),
m => m.channel.send('x', { code: true }),
m => m.channel.send('1', { code: 'js' }),
m => m.channel.send('x', { code: '' }),
m => m.channel.send('x', { embed: { description: 'a' } }),
m => m.channel.send({ embed: { description: 'a' } }),
m => m.channel.send({ files: [{ attachment: linkA }] }),
m =>
m.channel.send({
embed: { description: 'a' },
files: [{ attachment: linkA, name: 'xyz.png' }],
}),
m => m.channel.send('x', embed().setDescription('a')),
m => m.channel.send(embed().setDescription('a')),
m => m.channel.send({ embed: embed().setDescription('a') }),
m => m.channel.send([embed().setDescription('a'), embed().setDescription('b')]),
m => m.channel.send('x', attach(linkA)),
m => m.channel.send(attach(linkA)),
m => m.channel.send({ files: [linkA] }),
m => m.channel.send({ files: [attach(linkA)] }),
async m => m.channel.send(attach(await buffer(linkA))),
async m => m.channel.send({ files: [await buffer(linkA)] }),
async m => m.channel.send({ files: [{ attachment: await buffer(linkA) }] }),
m => m.channel.send([attach(linkA), attach(linkB)]),
m => m.channel.send({ embed: { description: 'a' } }).then(m2 => m2.edit('x')),
m => m.channel.send(embed().setDescription('a')).then(m2 => m2.edit('x')),
m => m.channel.send({ embed: embed().setDescription('a') }).then(m2 => m2.edit('x')),
m => m.channel.send('x').then(m2 => m2.edit({ embed: { description: 'a' } })),
m => m.channel.send('x').then(m2 => m2.edit(embed().setDescription('a'))),
m => m.channel.send('x').then(m2 => m2.edit({ embed: embed().setDescription('a') })),
m => m.channel.send({ embed: { description: 'a' } }).then(m2 => m2.edit({ embed: null })),
m => m.channel.send(embed().setDescription('a')).then(m2 => m2.edit({ embed: null })),
m => m.channel.send('x', [embed().setDescription('a'), attach(linkB)]),
m => m.channel.send('x', [attach(linkA), attach(linkB)]),
m => m.channel.send([embed().setDescription('a'), attach(linkB)]),
m =>
m.channel.send({
embed: embed().setImage('attachment://two.png'),
files: [attach(linkB, 'two.png')],
}),
m =>
m.channel.send({
embed: embed()
.setImage('attachment://two.png')
.attachFiles([attach(linkB, 'two.png')]),
}),
m => m.channel.send('x', attach(fileA)),
m => m.channel.send({ files: [fileA] }),
m => m.channel.send(attach(fileA)),
async m => m.channel.send({ files: [await read(fileA)] }),
m => m.channel.send('x', attach(readStream(fileA))),
m => m.channel.send({ files: [readStream(fileA)] }),
m => m.channel.send({ files: [{ attachment: readStream(fileA) }] }),
m => m.channel.send('Done!'),
];
client.on('messageCreate', async message => {
if (message.author.id !== owner) return;
const match = message.content.match(/^do (.+)$/);
if (match?.[1] === 'it') {
/* eslint-disable no-await-in-loop */
for (const [i, test] of tests.entries()) {
await message.channel.send(`**#${i}**\n\`\`\`js\n${test.toString()}\`\`\``);
await test(message).catch(e => message.channel.send(`Error!\n\`\`\`\n${e}\`\`\``));
await sleep(1_000);
}
/* eslint-enable no-await-in-loop */
} else if (match) {
const n = parseInt(match[1]) || 0;
const test = tests.slice(n)[0];
const i = tests.indexOf(test);
await message.channel.send(`**#${i}**\n\`\`\`js\n${test.toString()}\`\`\``);
await test(message).catch(e => message.channel.send(`Error!\n\`\`\`\n${e}\`\`\``));
}
});
client.login(token);
// eslint-disable-next-line no-console
process.on('unhandledRejection', console.error);

View File

@@ -0,0 +1,37 @@
'use strict';
const process = require('node:process');
const { setTimeout } = require('node:timers');
const { token } = require('./auth.json');
const { Client, Intents } = require('../src');
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
shards: process.argv[2],
shardCount: process.argv[3],
});
client.on('messageCreate', msg => {
if (msg.content.startsWith('?eval') && msg.author.id === '66564597481480192') {
try {
const com = eval(msg.content.split(' ').slice(1).join(' '));
msg.channel.send(com, { code: true });
} catch (e) {
msg.channel.send(e, { code: true });
}
}
});
process.send(123);
client.on('ready', () => {
console.log('Ready', client.options.shards);
if (client.options.shards === 0) {
setTimeout(() => {
console.log('kek dying');
client.destroy();
}, 5_000);
}
});
client.login(token).catch(console.error);

View File

@@ -0,0 +1,11 @@
'use strict';
const process = require('node:process');
const { token } = require('./auth');
const Discord = require('../src');
const sharder = new Discord.ShardingManager(`${process.cwd()}/test/shard.js`, { token, respawn: false });
sharder.on('launch', shard => console.log(`launched ${shard.id}`));
sharder.spawn();

View File

@@ -0,0 +1,27 @@
'use strict';
const { token } = require('./auth');
const { Client } = require('../src');
const client = new Client({ intents: ['GUILDS', 'GUILD_MESSAGES'] });
client
.on('ready', () => console.log('ready'))
.on('messageCreate', async message => {
try {
const templates = await message.guild.fetchTemplates();
if (!templates.size) {
console.log('no templates');
} else {
const guild = await templates.first().createGuild('guild name');
console.log(`created guild with id ${guild.id}`);
await guild.delete();
console.log('deleted guild');
}
} catch (error) {
console.error(error);
} finally {
client.destroy();
}
})
.login(token)
.catch(console.error);

View File

@@ -0,0 +1,55 @@
'use strict';
const process = require('node:process');
const { token, prefix, owner } = require('./auth.js');
const { Client, Intents } = require('../src');
// eslint-disable-next-line no-console
const log = (...args) => console.log(process.uptime().toFixed(3), ...args);
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
shardCount: 2,
});
client.on('debug', log);
client.on('ready', () => {
log('READY', client.user.tag, client.user.id);
});
client.on('rateLimit', log);
client.on('error', console.error);
const commands = {
eval: message => {
if (message.author.id !== owner) return;
let res;
try {
res = eval(message.content);
if (typeof res !== 'string') res = require('node:util').inspect(res);
} catch (err) {
// eslint-disable-next-line no-console
console.error(err.stack);
res = err.message;
}
message.channel.send(res, { code: 'js' });
},
ping: message => message.channel.send('pong'),
};
client.on('messageCreate', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
message.content = message.content.replace(prefix, '').trim().split(' ');
const command = message.content.shift();
message.content = message.content.join(' ');
// eslint-disable-next-line no-console
console.log('COMMAND', command, message.content);
if (command in commands) commands[command](message);
});
client.login(token);
// eslint-disable-next-line no-console
process.on('unhandledRejection', console.error);

View File

@@ -0,0 +1,67 @@
'use strict';
const process = require('node:process');
const { token, prefix, owner } = require('./auth.js');
const { Client, Options, Intents, Formatters } = require('../src');
// eslint-disable-next-line no-console
const log = (...args) => console.log(process.uptime().toFixed(3), ...args);
const client = new Client({
// 😏
intents: Object.values(Intents.FLAGS).reduce((acc, p) => acc | p, 0),
makeCache: Options.cacheWithLimits({
MessageManager: 10,
PresenceManager: 10,
UserManager: {
maxSize: 1,
keepOverLimit: v => v.id === client.user.id,
},
GuildMemberManager: {
maxSize: 1,
keepOverLimit: v => v.id === client.user.id,
},
}),
});
client.on('debug', log);
client.on('ready', () => {
log('READY', client.user.tag, client.user.id);
});
client.on('rateLimit', log);
client.on('error', console.error);
const commands = {
eval: message => {
if (message.author.id !== owner) return;
let res;
try {
res = eval(message.content);
if (typeof res !== 'string') res = require('node:util').inspect(res);
} catch (err) {
// eslint-disable-next-line no-console
console.error(err.stack);
res = err.message;
}
message.channel.send(Formatters.codeBlock(res));
},
ping: message => message.channel.send('pong'),
};
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
message.content = message.content.replace(prefix, '').trim().split(' ');
const command = message.content.shift();
message.content = message.content.join(' ');
// eslint-disable-next-line no-console
console.log('COMMAND', command, message.content);
if (command in commands) commands[command](message);
});
client.login(token);
// eslint-disable-next-line no-console
process.on('unhandledRejection', console.error);

View File

@@ -0,0 +1,127 @@
'use strict';
const fs = require('node:fs');
const path = require('node:path');
const { setTimeout: sleep } = require('node:timers/promises');
const util = require('node:util');
const fetch = require('node-fetch');
const { owner, token, webhookChannel, webhookToken } = require('./auth.js');
const { Client, Intents, MessageAttachment, MessageEmbed, WebhookClient } = require('../src');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const buffer = l => fetch(l).then(res => res.buffer());
const read = util.promisify(fs.readFile);
const readStream = fs.createReadStream;
const linkA = 'https://lolisafe.moe/iiDMtAXA.png';
const linkB = 'https://lolisafe.moe/9hSpedPh.png';
const fileA = path.join(__dirname, 'blobReach.png');
const embed = () => new MessageEmbed();
const attach = (attachment, name) => new MessageAttachment(attachment, name);
const tests = [
(m, hook) => hook.send('x'),
(m, hook) => hook.send('x', { code: true }),
(m, hook) => hook.send('1', { code: 'js' }),
(m, hook) => hook.send('x', { code: '' }),
(m, hook) => hook.send({ embeds: [{ description: 'a' }] }),
(m, hook) => hook.send({ files: [{ attachment: linkA }] }),
(m, hook) =>
hook.send({
embeds: [{ description: 'a' }],
files: [{ attachment: linkA, name: 'xyz.png' }],
}),
(m, hook) => hook.send('x', embed().setDescription('a')),
(m, hook) => hook.send(embed().setDescription('a')),
(m, hook) => hook.send({ embeds: [embed().setDescription('a')] }),
(m, hook) => hook.send([embed().setDescription('a'), embed().setDescription('b')]),
(m, hook) => hook.send('x', attach(linkA)),
(m, hook) => hook.send(attach(linkA)),
(m, hook) => hook.send({ files: [linkA] }),
(m, hook) => hook.send({ files: [attach(linkA)] }),
async (m, hook) => hook.send(attach(await buffer(linkA))),
async (m, hook) => hook.send({ files: [await buffer(linkA)] }),
async (m, hook) => hook.send({ files: [{ attachment: await buffer(linkA) }] }),
(m, hook) => hook.send([attach(linkA), attach(linkB)]),
(m, hook) => hook.send(embed().setDescription('a')),
(m, hook) => hook.send({ embeds: [{ description: 'a' }] }),
(m, hook) => hook.send(embed().setDescription('a')),
(m, hook) => hook.send('x', [embed().setDescription('a'), attach(linkB)]),
(m, hook) => hook.send('x', [attach(linkA), attach(linkB)]),
(m, hook) => hook.send([embed().setDescription('a'), attach(linkB)]),
(m, hook) =>
hook.send({
embeds: [embed().setImage('attachment://two.png')],
files: [attach(linkB, 'two.png')],
}),
(m, hook) =>
hook.send({
embeds: [
embed()
.setImage('attachment://two.png')
.attachFiles([attach(linkB, 'two.png')]),
],
}),
async (m, hook) =>
hook.send(['x', 'y', 'z'], {
code: 'js',
embeds: [
embed()
.setImage('attachment://two.png')
.attachFiles([attach(linkB, 'two.png')]),
],
files: [{ attachment: await buffer(linkA) }],
}),
(m, hook) => hook.send('x', attach(fileA)),
(m, hook) => hook.send({ files: [fileA] }),
(m, hook) => hook.send(attach(fileA)),
async (m, hook) => hook.send({ files: [await read(fileA)] }),
(m, hook) => hook.send('x', attach(readStream(fileA))),
(m, hook) => hook.send({ files: [readStream(fileA)] }),
(m, hook) => hook.send({ files: [{ attachment: readStream(fileA) }] }),
(m, hook) => hook.send('Done!'),
];
client.on('messageCreate', async message => {
if (message.author.id !== owner) return;
const match = message.content.match(/^do (.+)$/);
const hooks = [
{ type: 'WebhookClient', hook: new WebhookClient({ id: webhookChannel, token: webhookToken }) },
{ type: 'TextChannel#fetchWebhooks', hook: await message.channel.fetchWebhooks().then(x => x.first()) },
{ type: 'Guild#fetchWebhooks', hook: await message.guild.fetchWebhooks().then(x => x.first()) },
];
if (match?.[1] === 'it') {
/* eslint-disable no-await-in-loop */
for (const { type, hook } of hooks) {
for (const [i, test] of tests.entries()) {
await message.channel.send(`**#${i}-Hook: ${type}**\n\`\`\`js\n${test.toString()}\`\`\``);
await test(message, hook).catch(e => message.channel.send(`Error!\n\`\`\`\n${e}\`\`\``));
await sleep(1_000);
}
}
/* eslint-enable no-await-in-loop */
} else if (match) {
const n = parseInt(match[1]) || 0;
const test = tests.slice(n)[0];
const i = tests.indexOf(test);
/* eslint-disable no-await-in-loop */
for (const { type, hook } of hooks) {
await message.channel.send(`**#${i}-Hook: ${type}**\n\`\`\`js\n${test.toString()}\`\`\``);
await test(message, hook).catch(e => message.channel.send(`Error!\n\`\`\`\n${e}\`\`\``));
}
/* eslint-enable no-await-in-loop */
}
});
client.login(token);