-
Notifications
You must be signed in to change notification settings - Fork 10
/
discord_mod.js
138 lines (125 loc) · 4.05 KB
/
discord_mod.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Discord Mod. An extension of the discord.js module to streamline usage of this bot.
*/
import Discord from 'discord.js'
import options from './config/options.js'
import Game from './games/_Game/classes/Game.js'
import logger from 'gamebot/logger'
/**
* Accesses and sets the prefix for a specific channel, regardless of channel caching
*/
Object.defineProperty(Discord.BaseChannel.prototype, 'prefix', {
get() {
return this.client.commandHandler.getPrefix(this) || options.prefix
},
set(prefix) {
if(this.guild)
this.client.commandHandler.prefixes.set(this.guild.id, prefix)
},
enumerable: true
})
/**
* Accesses the game for the current channel
*/
Object.defineProperty(Discord.BaseChannel.prototype, 'game', {
get() {
return this.client.gameManager.games.get(this.id)
},
set(value) {
if(value == null) {
return this.client.gameManager.games.delete(this.id)
} else if (value instanceof Game) {
return this.client.gameManager.games.set(this.id, value)
}
},
enumerable: true
})
/**
* Asynchronous version of {@link https://discord.js.org/#/docs/main/11.5.1/class/TextChannel?scrollTo=startTyping|Discord.TextChannel.startTyping()}
* @returns {Promise<Boolean>} Always resolves to true.
*/
Discord.BaseChannel.prototype.startTypingAsync = function (channelResolvable) {
return new Promise((resolve, reject) => {
try {
channelResolvable.startTyping()
resolve(true)
} catch (err) {
reject(err)
}
})
}
/**
* Easily send a message to a TextChannel or DMChannel as an embed.
* @param {String} description The description field of the embed.
* @param {String} title The title of the embed.
* @param {Discord.ColorResolvable} color The color of the embed.
* @see {@link https://discord.js.org/#/docs/main/11.5.1/typedef/ColorResolvable|Discord.ColorResolvable}
* @returns {Promise<Discord.Message>}
*/
Discord.BaseChannel.prototype.sendEmbed = function(description, title, embedColor) {
return this.send({
embeds: [{
color: embedColor || 3789311,
title,
description
}]
})
}
/**
* Creates a new user object in the database.
* @returns {Promise<Object>} The data the user was initialized with.
* @example
* user.createDBInfo()
* .then(info => console.log(`User was created with ${info.balance} credits`))
* .catch(logger.error.bind(logger))
*/
Discord.BaseChannel.prototype.createDBInfo = function() {
return new Promise(this.client.dbClient.createDBInfo(this.id))
}
/**
* @typedef UserData
* @type {Object}
* @property {String} userID The user's ID
* @property {Array.<String>} unlockedItems The list of unlocked items, categorized by ID
*/
/**
* Fetches a user's database information.
* @returns {Promise<UserData>}
*/
Discord.User.prototype.fetchDBInfo = function() {
return this.client.dbClient.fetchDBInfo(this.id)
}
/**
* Sees if a user has an item in their inventory.
* @returns {Boolean}
*/
Discord.User.prototype.hasItem = function (itemID) {
return this.client.dbClient.hasItem(this.id, itemID)
}
/**
* Updates the current status message.
*/
Discord.Client.prototype.updateStatus = async function(itemID) {
// Check if we don't have access to the status channel
if(this.latestStatus?.error) {
return null
}
// Check if we have a cached status
if(this.latestStatus) {
// Check if we need to update the status
if(Date.now() - this.latestStatus.lastUpdated < options.status.updateInterval)
return this.latestStatus
}
// If this shard is unable to find the status channel once, it will never find it
let statusChannel = await this.channels.fetch(options.statusChannel)
if(statusChannel) {
let message = (await statusChannel.messages.fetch({ limit: 1 }).catch(logger.error.bind(logger))).first()
this.latestStatus = { content: message.content, date: message.createdAt.toLocaleDateString(), lastUpdated: Date.now() }
} else {
this.latestStatus = { error: 'Unable to find status channel' }
}
return this.latestStatus
}
const Collection = Discord.Collection
export { Collection }
export default Discord