UNPKG

3.69 kBPlain TextView Raw
1const log = logger.withScope('discord:login')
2
3import { Client, CategoryChannel, Collection, DMChannel, TextChannel } from 'discord.js'
4import FakeClient from '../dummy/discord'
5import GuildArray from '../types/GuildArray'
6
7export default () => {
8 let client: Client | FakeClient
9 if (config.discord.token === 'dummy') {
10 client = new FakeClient()
11 } else {
12 client = new Client()
13 }
14
15 // log in to discord
16 log.start('Logging in to Discord...')
17 return client.login(config.discord.token).then(async () => {
18 log.success('Logged in to Discord')
19 global.discord = {
20 client,
21 guilds: new GuildArray(...client.guilds.array()),
22 channels: {},
23 webhooks: new Collection(),
24 getChannel (id: string) {
25 return client.channels.get(id) as TextChannel
26 }
27 }
28
29 // if bot isn't added to any guilds, send error
30 if (client.guilds.size === 0 && client instanceof Client) {
31 throw new Error(`No guilds added!
32You can add a bot to your guild here:
33https://discordapp.com/api/oauth2/authorize?client_id=${client.user.id}&permissions=537390096&scope=bot
34It's not an error... unless you added the bot to your guild already.`)
35 }
36
37 // set guild as a global variable, if it's specified in arguments then get it by name, if not then get the first one
38 if (config.discord.guild) {
39 const guild = client.guilds.find(guild => guild.name.toLowerCase() === config.discord.guild.toLowerCase() || guild.id === config.discord.guild)
40 if (!guild) throw new Error(`Guild ${config.discord.guild} was not found!`)
41 // move OUR guild to the front.
42 discord.guilds = new GuildArray(
43 guild,
44 ...discord.guilds.filter(_guild => _guild.id !== guild.id)
45 )
46 }
47
48 log.debug('Found Discord guild(s)')
49 log.trace('guilds', discord.guilds, 1)
50
51 // if user put category name in the config
52 if (config.discord.category) {
53 // get category
54 const category = discord.guilds[0].channels.find(channel =>
55 (channel.name.toLowerCase() === config.discord.category.toLowerCase() || channel.id === config.discord.category) &&
56 (channel.type === null || channel.type === 'category')
57 )
58 // if category is missing, crash
59 if (!category) throw new Error(`Category ${config.discord.category} was not found!`)
60 log.debug('Got Discord category')
61 log.trace('category', category)
62 discord.category = category as CategoryChannel
63 }
64
65 log.debug('Got Discord channels list')
66
67 discord.channels = {}
68
69 if (config.channels.error) {
70 if (!Array.isArray(config.channels.error)) config.channels.error = [ config.channels.error ]
71 discord.channels.error = await getChannels(client, config.channels.error)
72 }
73
74 if (config.channels.command) {
75 if (!Array.isArray(config.channels.command)) config.channels.command = [ config.channels.command ]
76 discord.channels.command = await getChannels(client, config.channels.command)
77 }
78
79 client.on('error', (err: Error) => { throw err })
80
81 return client
82 })
83}
84async function getChannels (client: Client | FakeClient, channels: string[]): Promise<(TextChannel | DMChannel)[]> {
85 return (
86 await Promise.all(channels.map(id => getChannel(client, id)))
87 ).filter(Boolean) as (TextChannel | DMChannel)[]
88}
89async function getChannel (client: Client | FakeClient, channelID: string): Promise<TextChannel | DMChannel | undefined> {
90 if (discord.client.users.has(channelID)) return client.users.get(channelID)!!.createDM()
91 if (discord.client.channels.has(channelID)) return client.channels.get(channelID) as TextChannel | DMChannel
92 log.warn(`Channel/user ${channelID} not found.`)
93}