@@ -15,6 +15,7 @@ import {
1515 GuildMember ,
1616 InteractionContextType ,
1717 MessageFlags ,
18+ TextChannel ,
1819 type ApplicationCommandOptionData ,
1920 type CacheType ,
2021 type CommandInteraction ,
@@ -37,6 +38,9 @@ import {
3738 discordCheckIfDmChannelExists ,
3839 discordGetAllTrackedInGuild ,
3940 discordRemoveGuildTrackingChannel ,
41+ discordUpdateSubscriptionAddChannel ,
42+ discordUpdateSubscriptionCheckGuild ,
43+ discordUpdateSubscriptionRemoveChannel ,
4044} from "./db/discord" ;
4145import {
4246 Platform ,
@@ -69,7 +73,7 @@ interface Command {
6973}
7074
7175// Context 2: Interaction can be used within Group DMs and DMs other than the app's bot user
72- // /track, /tracked and /untracked can't be used in these contexts
76+ // /track, /tracked, /untracked and /updates can't be used in these contexts
7377const commands : Record < string , Command > = {
7478 ping : {
7579 data : {
@@ -708,7 +712,7 @@ const commands: Record<string, Command> = {
708712 ) {
709713 await interaction . reply ( {
710714 flags : MessageFlags . Ephemeral ,
711- content : `Started tracking the streamer ${ platformUserId } ( ${ platformUserId } ) in <#${ targetChannel ?. id } >!` ,
715+ content : `Started tracking the streamer ${ streamerName } in <#${ targetChannel ?. id } >!` ,
712716 } ) ;
713717 } else {
714718 await interaction . reply ( {
@@ -1174,6 +1178,177 @@ const commands: Record<string, Command> = {
11741178 } ) ;
11751179 } ,
11761180 } ,
1181+ updates : {
1182+ data : {
1183+ name : "updates" ,
1184+ description : "Enable or disable updates for Feedr in this channel" ,
1185+ integration_types : [ 0 , 1 ] ,
1186+ contexts : [ 0 , 1 ] ,
1187+ options : [
1188+ {
1189+ name : "state" ,
1190+ description : "Choose whether to enable or disable updates" ,
1191+ type : ApplicationCommandOptionType . String ,
1192+ required : true ,
1193+ choices : [
1194+ {
1195+ name : "Enable" ,
1196+ value : "enable" ,
1197+ } ,
1198+ {
1199+ name : "Disable" ,
1200+ value : "disable" ,
1201+ } ,
1202+ ] ,
1203+ } ,
1204+ ] ,
1205+ } ,
1206+ execute : async ( interaction : CommandInteraction ) => {
1207+ const isDm = ! interaction . inGuild ( ) ;
1208+
1209+ const channelId = interaction . channelId ;
1210+ const guildId = isDm ? channelId : interaction . guildId ;
1211+
1212+ if ( ! isDm && ! guildId ) {
1213+ await interaction . reply ( {
1214+ flags : MessageFlags . Ephemeral ,
1215+ content : "An error occurred! Please report" ,
1216+ } ) ;
1217+
1218+ return ;
1219+ }
1220+
1221+ // Check type of the channel
1222+ const targetChannel = await client . channels . fetch ( channelId ) ;
1223+
1224+ if (
1225+ targetChannel &&
1226+ ( targetChannel . type === ChannelType . GuildText ||
1227+ targetChannel . type === ChannelType . GuildAnnouncement )
1228+ ) {
1229+ if (
1230+ ! isDm &&
1231+ ! interaction . memberPermissions ?. has (
1232+ PermissionFlagsBits . ManageChannels ,
1233+ )
1234+ ) {
1235+ // Check the permissions of the user
1236+ await interaction . reply ( {
1237+ flags : MessageFlags . Ephemeral ,
1238+ content :
1239+ "You do not have the permission to manage channels!" ,
1240+ } ) ;
1241+
1242+ return ;
1243+ }
1244+ }
1245+
1246+ // Check the permissions of the bot in the channel
1247+ const botMember = isDm
1248+ ? null
1249+ : await interaction . guild ?. members . fetchMe ( ) ;
1250+
1251+ if (
1252+ botMember &&
1253+ ! botMember
1254+ . permissionsIn ( channelId )
1255+ . has ( PermissionFlagsBits . SendMessages )
1256+ ) {
1257+ await interaction . reply ( {
1258+ flags : MessageFlags . Ephemeral ,
1259+ content :
1260+ "I do not have permission to send messages in that channel!" ,
1261+ } ) ;
1262+
1263+ return ;
1264+ }
1265+
1266+ // Get the current state from the database
1267+ const currentDatabaseState =
1268+ await discordUpdateSubscriptionCheckGuild ( guildId ) ;
1269+
1270+ if ( ! currentDatabaseState || ! currentDatabaseState . success ) {
1271+ await interaction . reply ( {
1272+ flags : MessageFlags . Ephemeral ,
1273+ content :
1274+ "An error occurred while trying to get the current update state from the database! Please report this error!" ,
1275+ } ) ;
1276+
1277+ return ;
1278+ }
1279+
1280+ const currentState = Boolean (
1281+ currentDatabaseState . data [ 0 ] . feedrUpdatesChannelId ,
1282+ ) ;
1283+ const desiredState = Boolean (
1284+ interaction . options . get ( "state" ) ?. value === "enable" ,
1285+ ) ;
1286+
1287+ if ( currentState === desiredState ) {
1288+ await interaction . reply ( {
1289+ flags : MessageFlags . Ephemeral ,
1290+ content : `Updates are already ${
1291+ desiredState ? "enabled" : "disabled"
1292+ } in this channel!`,
1293+ } ) ;
1294+
1295+ return ;
1296+ }
1297+
1298+ if ( desiredState ) {
1299+ // Enable updates
1300+ const updateSuccess = await discordUpdateSubscriptionAddChannel (
1301+ guildId ,
1302+ channelId ,
1303+ ) ;
1304+
1305+ if ( ! updateSuccess || ! updateSuccess . success ) {
1306+ await interaction . reply ( {
1307+ flags : MessageFlags . Ephemeral ,
1308+ content :
1309+ "An error occurred while trying to enable updates in this channel! Please report this error!" ,
1310+ } ) ;
1311+
1312+ return ;
1313+ }
1314+
1315+ await client . channels
1316+ . fetch ( channelId )
1317+ . then ( async ( channel ) => {
1318+ if ( channel ?. isTextBased ( ) ) {
1319+ await ( channel as TextChannel ) . send ( {
1320+ content : `Updates have been successfully enabled in this channel!` ,
1321+ } ) ;
1322+ }
1323+ } )
1324+ . catch ( console . error ) ;
1325+
1326+ await interaction . reply ( {
1327+ flags : MessageFlags . Ephemeral ,
1328+ content :
1329+ "If a test message was sent, updates are enabled! If not, please report this as an error!" ,
1330+ } ) ;
1331+ } else {
1332+ // Disable updates
1333+ const updateSuccess =
1334+ await discordUpdateSubscriptionRemoveChannel ( guildId ) ;
1335+
1336+ if ( ! updateSuccess || ! updateSuccess . success ) {
1337+ await interaction . reply ( {
1338+ flags : MessageFlags . Ephemeral ,
1339+ content :
1340+ "An error occurred while trying to disable updates in this channel! Please report this error!" ,
1341+ } ) ;
1342+
1343+ return ;
1344+ }
1345+
1346+ await interaction . reply ( {
1347+ content : `Successfully disabled updates in <#${ channelId } >!` ,
1348+ } ) ;
1349+ }
1350+ } ,
1351+ } ,
11771352} ;
11781353
11791354// Convert commands to a Map
0 commit comments