Skip to content

Commit 11412a6

Browse files
committed
Fix follow commands
1 parent 6164d1c commit 11412a6

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

src/commands/news/follow.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { CommandInteraction, Message, TextChannel } from "discord.js"
1+
import { CommandInteraction, Message } from "discord.js"
22

3-
import Command from "../../utils/Command"
3+
import config from "../../data/config.json"
44
import client from "../../main"
5+
import Command from "../../utils/Command"
56
import { CommandSource, FollowCategory, SendMessage } from "../../utils/Types"
6-
import { createTable, getUserID, sendMessage } from "../../utils/Utils"
7-
import config from "../../data/config.json"
7+
import { createTable, getUserID, isNewsable, sendMessage } from "../../utils/Utils"
88

99
const descriptions: { [x in FollowCategory]: string } = {
1010
"events": "Get event reminders",
@@ -100,7 +100,7 @@ Example of adding news: \`${config.prefix}follow add news\``,
100100

101101
async runInteraction(source: CommandInteraction): Promise<SendMessage | undefined> {
102102
const channel = await source.channel?.fetch()
103-
if (!(channel instanceof TextChannel) || source.guild == null)
103+
if (!channel || !isNewsable(channel) || source.guild == null)
104104
return sendMessage(source, "This command can only be executed in guild channels. You can invite this bot in your own server via `.invite`", undefined, true)
105105

106106
if (typeof source.member?.permissions == "string")
@@ -123,7 +123,7 @@ Example of adding news: \`${config.prefix}follow add news\``,
123123

124124
async runMessage(source: Message, args: string[]): Promise<SendMessage | undefined> {
125125
const channel = await source.channel?.fetch()
126-
if (!(channel instanceof TextChannel) || source.guild == null)
126+
if (!channel || !isNewsable(channel) || source.guild == null)
127127
return sendMessage(source, "This command can only be executed in guild channels. You can invite this bot in your own server via `.invite`", undefined, true)
128128

129129
if (!source.member?.permissions.has("ADMINISTRATOR") && !config.admins.includes(getUserID(source)))
@@ -154,7 +154,8 @@ Example of adding news: \`${config.prefix}follow add news\``,
154154
}
155155

156156
async runList(source: CommandSource, category?: FollowCategory | null): Promise<SendMessage | undefined> {
157-
if (!(source.channel instanceof TextChannel) || source.guild == null)
157+
const channel = await source.channel?.fetch()
158+
if (!channel || !isNewsable(channel) || source.guild == null)
158159
return sendMessage(source, "Unable to check channel", undefined, true)
159160

160161
const { followManager } = client
@@ -165,7 +166,7 @@ Example of adding news: \`${config.prefix}follow add news\``,
165166
for (const follow of following)
166167
try {
167168
const channel = await client.channels.fetch(follow.channelID)
168-
if (channel instanceof TextChannel)
169+
if (isNewsable(channel))
169170
channels.push({
170171
channelname: channel.name,
171172
category: follow.category
@@ -183,7 +184,7 @@ ${createTable(
183184
))}\`\`\``, undefined, true)
184185
}
185186

186-
const follows = followManager.getFollows(source.channel, category)
187+
const follows = followManager.getFollows(channel, category)
187188
if (follows.length == 0) return sendMessage(source, `Not following ${category}`, undefined, true)
188189
return sendMessage(source, follows.map(k => `Following ${category} since ${new Date(k.addedOn).toLocaleString("en-UK", {
189190
timeZone: "GMT",
@@ -198,23 +199,25 @@ ${createTable(
198199
}
199200

200201
async runUnfollow(source: CommandSource, category: FollowCategory): Promise<SendMessage | undefined> {
201-
if (!(source.channel instanceof TextChannel) || source.guild == null)
202+
const channel = await source.channel?.fetch()
203+
if (!channel || !isNewsable(channel) || source.guild == null)
202204
return sendMessage(source, "Unable to unfollow in this channel", undefined, true)
203205

204206
const { followManager } = client
205207

206-
followManager.unfollow(source.channel, category)
208+
followManager.unfollow(channel, category)
207209

208-
return sendMessage(source, `Unfollowed ${category} in <#${source.channel.id}>`, undefined, true)
210+
return sendMessage(source, `Unfollowed ${category} in <#${channel.id}>`, undefined, true)
209211
}
210212
async runFollow(source: CommandSource, category: FollowCategory): Promise<SendMessage | undefined> {
211-
if (!(source.channel instanceof TextChannel) || source.guild == null)
213+
const channel = await source.channel?.fetch()
214+
if (!channel || !isNewsable(channel) || source.guild == null)
212215
return sendMessage(source, "Unable to follow in this channel", undefined, true)
213216

214217
const { followManager } = client
215218

216-
followManager.addFollow(source.guild, source.channel, category, getUserID(source))
219+
followManager.addFollow(source.guild, channel, category, getUserID(source))
217220

218-
return sendMessage(source, `Now following ${category} in <#${source.channel.id}>`, undefined, true)
221+
return sendMessage(source, `Now following ${category} in <#${channel.id}>`, undefined, true)
219222
}
220223
}

src/utils/FollowManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export default class FollowManager {
5151

5252
private getFollowsStatement: SQLite.Statement
5353
private getFollowsInChannelStatement: SQLite.Statement
54-
getFollows(channel: Channel, category?: FollowCategory): Follower[] {
54+
getFollows(channel: { id: string }, category?: FollowCategory): Follower[] {
5555
if (category == undefined) {
5656
return this.getFollowsInChannelStatement.all({ channelID: channel.id })
5757
}

src/utils/Utils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CategoryChannel, ColorResolvable, Message, MessageActionRow, MessageButton, MessageComponentInteraction, MessageEmbed, Snowflake, User } from "discord.js"
1+
import { AnyChannel, CategoryChannel, ColorResolvable, GuildChannel, Message, MessageActionRow, MessageButton, MessageComponentInteraction, MessageEmbed, NewsChannel, Snowflake, TextBasedChannel, TextChannel, User } from "discord.js"
22
import log4js from "log4js"
33
import config from "./../data/config.json"
44
import client from "./../main"
@@ -565,6 +565,12 @@ export function isMessage(msg: SendMessage | CommandSource | undefined): msg is
565565
return msg instanceof Message
566566
}
567567

568+
export type NewsableChannel = NewsChannel | TextChannel
569+
export function isNewsable(channel: AnyChannel | GuildChannel | TextBasedChannel | null): channel is NewsableChannel {
570+
if (!channel) return false
571+
return channel.type == "GUILD_TEXT" || channel.type == "GUILD_NEWS" || channel.type == "GUILD_NEWS_THREAD"
572+
}
573+
568574
export function getUserID(source: CommandSource): string {
569575
return getUser(source).id
570576
}

0 commit comments

Comments
 (0)