harmony/src/test/index.ts

257 lines
8 KiB
TypeScript
Raw Normal View History

2020-12-02 12:29:52 +00:00
import {
Client,
Intents,
Message,
Member,
Role,
GuildChannels,
2020-12-02 12:29:52 +00:00
Embed,
Guild,
EveryChannelTypes,
ChannelTypes,
GuildTextChannel
} from '../../mod.ts'
2021-01-07 13:46:56 +00:00
import { Collector } from '../models/collectors.ts'
import { MessageAttachment } from '../structures/message.ts'
2021-01-25 14:02:43 +00:00
import { Permissions } from '../utils/permissions.ts'
import { TOKEN } from './config.ts'
const client = new Client({
2020-12-17 01:20:06 +00:00
// clientProperties: {
// browser: 'Discord iOS'
// }
// bot: false,
// cache: new RedisCacheAdapter({
// hostname: '127.0.0.1',
// port: 6379
2021-01-24 16:46:10 +00:00
// }), // Defaults to in-memory Caching
// shardCount: 2
2020-11-02 07:27:14 +00:00
})
client.on('ready', () => {
console.log(`[Login] Logged in as ${client.user?.tag}!`)
})
client.on('debug', console.log)
2020-12-02 12:29:52 +00:00
client.on('channelUpdate', (b: EveryChannelTypes, a: EveryChannelTypes) => {
if (b.type === ChannelTypes.GUILD_TEXT) {
const before = (b as unknown) as GuildTextChannel
const after = (a as unknown) as GuildTextChannel
2020-12-20 09:11:37 +00:00
before.send('', {
embed: new Embed({
title: 'Channel Update',
description: `Name Before: ${before.name}\nName After: ${after.name}`
2020-12-02 12:29:52 +00:00
})
2020-12-20 09:11:37 +00:00
})
2020-12-02 12:29:52 +00:00
}
})
client.on('messageCreate', async (msg: Message) => {
2020-11-03 07:12:22 +00:00
if (msg.author.bot === true) return
2020-12-17 01:20:06 +00:00
if (msg.stickers !== undefined) {
console.log(
`${msg.author.tag}: (Sticker)${msg.stickers.map(
(sticker) => `Name: ${sticker.name}, Tags: ${sticker.tags}`
)}`
)
} else {
console.log(`${msg.author.tag}: ${msg.content}`)
}
if (msg.content === '!ping') {
msg.reply(`Pong! Ping: ${client.ping}ms`)
} else if (msg.content === '!members') {
2020-12-19 07:58:40 +00:00
const col = await msg.guild?.members.array()
2020-12-02 12:29:52 +00:00
const data = col
2020-12-19 07:58:40 +00:00
?.map((c: Member, i: number) => {
2020-12-02 12:29:52 +00:00
return `${i + 1}. ${c.user.tag}`
})
.join('\n') as string
msg.channel.send('Member List:\n' + data)
} else if (msg.content === '!guilds') {
2020-11-03 07:12:22 +00:00
const guilds = await msg.client.guilds.collection()
2020-12-02 12:29:52 +00:00
msg.channel.send(
'Guild List:\n' +
(guilds
.array()
.map((c: Guild, i: number) => {
return `${i + 1}. ${c.name} - ${c.memberCount} members`
})
.join('\n') as string)
)
} else if (msg.content === '!roles') {
2020-11-03 07:12:22 +00:00
const col = await msg.guild?.roles.collection()
2020-12-02 12:29:52 +00:00
const data = col
?.array()
.map((c: Role, i: number) => {
return `${i + 1}. ${c.name}`
})
.join('\n') as string
msg.channel.send('Roles List:\n' + data)
} else if (msg.content === '!channels') {
2020-11-03 07:12:22 +00:00
const col = await msg.guild?.channels.array()
2020-12-02 12:29:52 +00:00
const data = col
?.map((c: GuildChannels, i: number) => {
2020-12-02 12:29:52 +00:00
return `${i + 1}. ${c.name}`
})
.join('\n') as string
msg.channel.send('Channels List:\n' + data)
2020-12-20 09:11:37 +00:00
} else if (msg.content === '!messages') {
const col = await msg.channel.messages.array()
const data = col
?.slice(-5)
.map((c: Message, i: number) => {
return `${i + 1}. ${c.content}`
})
.join('\n') as string
msg.channel.send('Top 5 Message List:\n' + data)
} else if (msg.content === '!editChannel') {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const channel = msg.channel as GuildTextChannel
const newChannel = await channel.edit({
name: 'gggg'
})
if (newChannel.name === 'gggg') {
msg.channel.send('Done!')
} else {
msg.channel.send('Failed...')
}
2020-12-24 00:59:01 +00:00
} else if (msg.content === '!react') {
2021-03-08 10:36:30 +00:00
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
msg.addReaction('😂')
} else if (msg.content === '!wait_for') {
msg.channel.send('Send anything!')
const [receivedMsg] = await client.waitFor(
'messageCreate',
(message) => message.author.id === msg.author.id
)
msg.channel.send(`Received: ${receivedMsg?.content}`)
2021-01-07 13:46:56 +00:00
} else if (msg.content.startsWith('!collect') === true) {
let count = parseInt(msg.content.replace(/\D/g, ''))
if (isNaN(count)) count = 5
await msg.channel.send(`Collecting ${count} messages for 5s`)
const coll = new Collector({
event: 'messageCreate',
filter: (m) => m.author.id === msg.author.id,
deinitOnEnd: true,
max: count,
timeout: 5000
})
coll.init(client)
coll.collect()
coll.on('start', () => msg.channel.send('[COL] Started'))
coll.on('end', () =>
msg.channel.send(`[COL] Ended. Collected Size: ${coll.collected.size}`)
)
coll.on('collect', (msg) =>
msg.channel.send(`[COL] Collect: ${msg.content}`)
)
} else if (msg.content === '!attach') {
msg.channel.send({
file: await MessageAttachment.load(
'https://cdn.discordapp.com/emojis/626139395623354403.png?v=1'
)
})
2021-01-28 14:25:37 +00:00
} else if (msg.content === '!emattach') {
msg.channel.send(
new Embed()
.attach(
await MessageAttachment.load(
'https://cdn.discordapp.com/emojis/626139395623354403.png?v=1',
'file1.png'
),
await MessageAttachment.load(
'https://cdn.discordapp.com/emojis/626139395623354403.png?v=1',
'file2.png'
)
)
.setImage('attachment://file1.png')
.setThumbnail('attachment://file2.png')
)
} else if (msg.content === '!textfile') {
msg.channel.send({
files: [
new MessageAttachment('hello.txt', 'world'),
new MessageAttachment('world.txt', 'hello')
]
})
2021-01-24 16:46:10 +00:00
} else if (msg.content === '!join') {
if (msg.member === undefined) return
const vs = await msg.guild?.voiceStates.get(msg.member.id)
if (typeof vs !== 'object') return
vs.channel?.join()
2021-01-25 14:02:43 +00:00
} else if (msg.content === '!getOverwrites') {
if (msg.channel.type !== ChannelTypes.GUILD_TEXT) {
return msg.channel.send("This isn't a guild text channel!")
}
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const overwrites = await (msg.channel as GuildTextChannel).overwritesFor(
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
msg.member as Member
)
msg.channel.send(
`Your permission overwrites:\n${overwrites
.map(
(over) =>
`ID: ${over.id}\nAllowed:\n${new Permissions(over.allow)
.toArray()
.join('\n')}\nDenied:\n${new Permissions(over.deny)
.toArray()
.join('\n')}`
)
.join('\n\n')}`
)
} else if (msg.content === '!getPermissions') {
if (msg.channel.type !== ChannelTypes.GUILD_TEXT) {
return msg.channel.send("This isn't a guild text channel!")
}
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const permissions = await (msg.channel as GuildTextChannel).permissionsFor(
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
msg.member as Member
)
msg.channel.send(`Your permissions:\n${permissions.toArray().join('\n')}`)
2021-02-23 04:07:27 +00:00
} else if (msg.content === '!addAllRoles') {
const roles = await msg.guild?.roles.array()
if (roles !== undefined) {
roles.forEach(async (role) => {
await msg.member?.roles.add(role)
console.log(role)
})
}
2021-02-23 04:47:14 +00:00
} else if (msg.content === '!createAndAddRole') {
if (msg.guild !== undefined) {
const role = await msg.guild.roles.create({
name: 'asdf',
permissions: 0
})
await msg.member?.roles.add(role)
}
} else if (msg.content === '!roles') {
let buf = 'Roles:'
if (msg.member === undefined) return
for await (const role of msg.member.roles) {
buf += `\n${role.name}`
}
msg.reply(buf)
2020-12-24 00:59:01 +00:00
}
})
client.on('messageReactionRemove', (reaction, user) => {
const msg = reaction.message
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (reaction.me && reaction.emoji.getEmojiString === '🤔') {
msg.removeReaction(reaction.emoji)
}
})
2021-01-24 16:46:10 +00:00
client.connect(TOKEN, Intents.None)
// OLD: Was a way to reproduce reconnect infinite loop
// setTimeout(() => {
// console.log('[DEBUG] Reconnect')
// client.gateway?.reconnect()
// }, 1000 * 4)