Skip to content

Commit

Permalink
Adds support to discord send to accept channel id
Browse files Browse the repository at this point in the history
Co-authored-by: Gerard <[email protected]>
  • Loading branch information
mikecmart and gdomenech98 committed Nov 12, 2024
1 parent 5f6539f commit 35ad726
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions packages/protolib/src/bundles/discord/context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getKey } from '../../keys/context';
import { Client, GatewayIntentBits } from 'discord.js'

const getToken = async (apiKey?): Promise<string> => {
if(apiKey) return apiKey
if (apiKey) return apiKey
try {
const token = await getKey({ key: "DISCORD_APP_TOKEN", token: getServiceToken() })
return token
Expand Down Expand Up @@ -33,35 +33,40 @@ type connectProps = {
apiKey?: string
}

const send = async ({channel, message, onSend}:{channel: any, message: string, onSend?: Function}) => {
channel.send(message)
if(onSend) onSend()
}

export const discord = {
connect : async ({onMessage, onConnect, onDisconnect, onError, apiKey}:connectProps) => {
connect: async ({ onMessage, onConnect, onDisconnect, onError, apiKey }: connectProps) => {
const key = await getToken(apiKey)
try {
await client.login(key)
client.on('disconnect', () => {
if(onDisconnect) onDisconnect(client)
if (onDisconnect) onDisconnect(client)
})

if(onConnect) onConnect(client)
if (onConnect) onConnect(client)
client.on('messageCreate', (message) => {
if(onMessage) onMessage(message)
if (onMessage) onMessage(message)
})

} catch (err) {
if(onError) onError(err)
if (onError) onError(err)
console.error("Bot initialization error", err)
}
},
send,
response: async ({message, response, onSend}:{message: any, response: string, onSend?: Function}) => {
return send({channel: message.channel, message: response, onSend})
send: async ({ channel, message, onSend }: { channel: any, message: string, onSend?: Function }) => {
if (channel && typeof channel === 'string') { // Checks that provided channel is channelId instead of channel object
try {
channel = await client.channels.fetch(channel); // Fetch channel object given channelId ('channel')
} catch (err) {
console.error("Error fetching channel: ", err);
}
}
channel.send(message)
if (onSend) onSend()
},
response: async ({ message, response, onSend }: { message: any, response: string, onSend?: Function }) => {
return send({ channel: message.channel, message: response, onSend })
},
readMessages: async ({channelId, limit = 0, onMessagesRead}: {channelId: string, limit?: number, onMessagesRead: Function}) => {
readMessages: async ({ channelId, limit = 0, onMessagesRead }: { channelId: string, limit?: number, onMessagesRead: Function }) => {
console.log("Reading messages from channel", channelId, "with limit:", limit);
try {
const channel = await client.channels.fetch(channelId);
Expand All @@ -77,11 +82,11 @@ export const discord = {
do {
// If limit is 0 (fetch all), request 100, otherwise fetch the minimum of 100 or the remaining messages
const fetchLimit = limit === 0 ? 100 : Math.min(remainingMessages, 100);

fetchedMessages = await channel.messages.fetch({ limit: fetchLimit, before: lastMessageId });
messages.push(...fetchedMessages.values());
lastMessageId = fetchedMessages.last()?.id;

// Update remaining messages count, if we are limiting
if (limit !== 0) {
remainingMessages -= fetchedMessages.size;
Expand Down

0 comments on commit 35ad726

Please sign in to comment.