harmony/test/index.ts

297 lines
9.2 KiB
TypeScript
Raw Normal View History

2020-12-02 12:29:52 +00:00
import {
Client,
Intents,
Message,
Member,
GuildChannels,
2020-12-02 12:29:52 +00:00
Embed,
Guild,
EveryChannelTypes,
ChannelTypes,
GuildTextChannel,
checkGuildTextBasedChannel,
2021-04-04 05:52:47 +00:00
Permissions,
Collector,
MessageAttachment,
OverrideType
} from '../mod.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') {
2021-04-17 08:17:46 +00:00
msg.reply(`Pong! Ping: ${client.gateway.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' +
2021-04-04 05:52:47 +00:00
(guilds
.array()
.map((c: Guild, i: number) => {
return `${i + 1}. ${c.name} - ${c.memberCount} members`
})
.join('\n') as string)
2020-12-02 12:29:52 +00:00
)
} 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-14 08:50:15 +00:00
msg.addReaction('a:programming:785013658257195008')
} 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') {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (!checkGuildTextBasedChannel(msg.channel)) {
2021-01-25 14:02:43 +00:00
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${over.allow
2021-01-25 14:02:43 +00:00
.toArray()
.join('\n')}\nDenied:\n${over.deny.toArray().join('\n')}`
2021-01-25 14:02:43 +00:00
)
.join('\n\n')}`
)
2021-02-17 08:30:07 +00:00
} else if (msg.content === '!perms') {
2021-01-25 14:02:43 +00:00
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
2021-03-26 07:08:32 +00:00
const permissions = await ((msg.channel as unknown) as GuildTextChannel).permissionsFor(
2021-01-25 14:02:43 +00:00
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
msg.member as Member
)
msg.channel.send(`Your permissions:\n${permissions.toArray().join('\n')}`)
} else if (msg.content === '!addBasicOverwrites') {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (!checkGuildTextBasedChannel(msg.channel)) {
return msg.channel.send("This isn't a guild text channel!")
}
if (msg.member !== undefined) {
await msg.channel.addOverwrite({
id: msg.member,
allow: Permissions.DEFAULT.toString()
})
msg.channel.send(`Done!`)
}
2021-03-19 15:48:24 +00:00
} else if (msg.content === '!updateBasicOverwrites') {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (!checkGuildTextBasedChannel(msg.channel)) {
return msg.channel.send("This isn't a guild text channel!")
}
if (msg.member !== undefined) {
await msg.channel.editOverwrite(
{
id: msg.member,
allow: Permissions.DEFAULT.toString()
},
{
2021-03-29 04:54:18 +00:00
allow: OverrideType.REMOVE
2021-03-19 15:48:24 +00:00
}
)
msg.channel.send(`Done!`)
}
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) {
2021-04-04 13:46:34 +00:00
buf += `\n${role.name === '@everyone' ? 'everyone' : role.name}`
}
msg.reply(buf)
2021-04-29 09:15:05 +00:00
} else if (msg.content === '!addrole') {
msg.member?.roles.add('837255383759716362')
2021-04-30 06:05:19 +00:00
} else if (msg.content === '!dm') {
console.log('wtf')
msg.author.send('UwU').then((m) => {
msg.reply(`Done, ${m.id}`)
})
2021-03-19 10:50:16 +00:00
} else if (msg.content === '!timer') {
msg.channel.send('3...').then((msg) => {
setTimeout(() => {
msg.edit('2...').then((msg) => {
setTimeout(() => {
msg.edit('1...').then((msg) => {
setTimeout(() => {
msg.edit('ok wut')
}, 1000)
})
}, 1000)
})
}, 1000)
})
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)