Replies: 1 comment
-
For a captcha message or a stock alert? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
`import {Link, Store} from '../store/model';
import Discord from 'discord.js';
import {config} from '../config';
import {logger} from '../logger';
import {DMPayload} from '.';
import {RawUserData} from 'discord.js/typings/rawDataTypes';
const {notifyGroup, webhooks, notifyGroupSeries} = config.notifications.discord;
const {pollInterval, responseTimeout, token, userId} = config.captchaHandler;
function getIdAndToken(webhook: string) {
const match = /.*/webhooks/(\d+)/(.+)/.exec(webhook);
if (!match) {
throw new Error('could not get discord webhook');
}
return {
id: match[1],
token: match[2],
};
}
export function sendDiscordMessage(link: Link, store: Store) {
if (webhooks.length > 0) {
logger.debug('↗ sending discord message');
}
}
export async function sendDMAsync(
payload: DMPayload
): Promise<Discord.Message | undefined> {
if (userId && token) {
logger.debug('↗ sending discord DM');
let client = undefined;
let dmChannel = undefined;
try {
client = await getDiscordClientAsync();
dmChannel = await getDMChannelAsync(client);
if (!dmChannel) {
logger.error('unable to get discord DM channel');
return;
}
let message: string | {} = payload;
if (payload.type === 'image') {
message = {
files: [
{
attachment: payload.content,
name: payload.content,
},
],
};
}
const result = await dmChannel.send(message);
logger.info('✔ discord DM sent');
return result;
} catch (error: unknown) {
logger.error("✖ couldn't send discord DM", error);
} finally {
client?.destroy();
}
} else {
logger.warn("✖ couldn't send discord DM, missing configuration");
}
return;
}
export async function getDMResponseAsync(
botMessage: Discord.Message | undefined,
timeout: number
): Promise {
if (!botMessage) return '';
const iterations = Math.max(Math.floor(timeout / pollInterval), 1);
let iteration = 0;
const client = await getDiscordClientAsync();
const dmChannel = await getDMChannelAsync(client);
if (!dmChannel) {
logger.error('unable to get discord DM channel');
return '';
}
return new Promise(resolve => {
let response = '';
const intervalId = setInterval(async () => {
const finish = (result: string) => {
client?.destroy();
clearInterval(intervalId);
resolve(result);
};
try {
iteration++;
const messages = await dmChannel.messages.fetch({
after: botMessage?.id,
});
const lastUserMessage = messages
.filter(message => message.reference?.messageId === botMessage?.id)
.last();
if (!lastUserMessage) {
if (iteration >= iterations) {
await dmChannel.send('Timed out waiting for response... 😿');
logger.error('✖ no response from user');
return finish(response);
}
} else {
response = lastUserMessage.cleanContent;
await lastUserMessage.react('✅');
logger.info(
✔ got captcha response: ${response}
);return finish(response);
}
} catch (error: unknown) {
logger.error("✖ couldn't get captcha response", error);
return finish(response);
}
}, pollInterval * 1000);
});
}
export async function sendDMAndGetResponseAsync(
payload: DMPayload,
timeout?: number
): Promise {
const message = await sendDMAsync(payload);
const response = await getDMResponseAsync(
message,
timeout || responseTimeout
);
return response;
}
async function getDiscordClientAsync() {
let clientInstance = undefined;
const allIntents = new Discord.Intents();
if (token) {
logger.info("Logging into discord:" + token);
clientInstance = new Discord.Client({intents: allIntents});
await clientInstance.login(token);
}
return clientInstance;
}
async function getDMChannelAsync(client?: Discord.Client) {
let dmChannelInstance = undefined;
if (userId && client) {
const user = await new Discord.User(client, {
id: userId,
} as RawUserData).fetch();
dmChannelInstance = await user.createDM();
}
return dmChannelInstance;
}
`
I can't figure out why where I have a new discord client created and it logins in then when it's ready it sends the message and it's saying client is not defined and I can't figure out how to get this to work properly?
Beta Was this translation helpful? Give feedback.
All reactions