mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 06:28:51 +00:00
refactor(server/discord): Refactored Discord reports to use webhooks (#158)
This commit is contained in:
1 file changed
+110
-47
+110
-47
@@ -1,4 +1,4 @@
|
||||
import axios from 'axios';
|
||||
import axios, { AxiosResponse } from 'axios';
|
||||
|
||||
import { Tweet, Profile } from '../../phone/src/common/typings/twitter';
|
||||
import { IMAGE_DELIMITER } from '../../phone/src/apps/twitter/utils/images';
|
||||
@@ -7,59 +7,122 @@ import { MarketplaceListing } from '../../phone/src/common/typings/marketplace';
|
||||
|
||||
const discordLogger = mainLogger.child({ module: 'discord' });
|
||||
|
||||
const DISCORD_TOKEN_ENV_VAR = 'discord_bot_token';
|
||||
const DISCORD_TOKEN = GetConvar(DISCORD_TOKEN_ENV_VAR, '');
|
||||
const DISCORD_CHANNEL_ENV_VAR = 'discord_channel_id';
|
||||
const DISCORD_CHANNEL_ID = GetConvar(DISCORD_CHANNEL_ENV_VAR, '');
|
||||
const DISCORD_HEADERS = { authorization: `Bot ${DISCORD_TOKEN}` };
|
||||
|
||||
const BASE_URL = 'https://discord.com/api';
|
||||
const POST_CHANNEL_URL = `${BASE_URL}/channels/${DISCORD_CHANNEL_ID}/messages`;
|
||||
|
||||
export async function reportTweetToDiscord(tweet: Tweet, reportingProfile: Profile): Promise<any> {
|
||||
if (!DISCORD_TOKEN || !DISCORD_CHANNEL_ID) {
|
||||
discordLogger.warn(`Got a request to report a tweet but discord is not
|
||||
configured. See README on how to configure discord endpoints.`);
|
||||
return;
|
||||
}
|
||||
const content = `**REPORTED TWEET**
|
||||
----------------------------------------------------------------------
|
||||
**Reporting User**: ${reportingProfile.profile_name} (${reportingProfile.id})
|
||||
**Reported User**: ${tweet.profile_name} (${tweet.profile_id})
|
||||
**Reported Tweet timestamp**: ${tweet.createdAt}
|
||||
----------------------------------------------------------------------
|
||||
**Reported Message**:
|
||||
|
||||
${tweet.message}
|
||||
|
||||
**Reported Images**:
|
||||
${tweet.images.split(IMAGE_DELIMITER).join('\n')}
|
||||
`;
|
||||
|
||||
return await axios.post(POST_CHANNEL_URL, { content }, { headers: DISCORD_HEADERS });
|
||||
const DISCORD_WEBHOOK_ENV_VAR = 'NPWD_DISCORD_TOKEN';
|
||||
const DISCORD_WEBHOOK = GetConvar(DISCORD_WEBHOOK_ENV_VAR, '');
|
||||
/**
|
||||
* https://discord.com/developers/docs/resources/channel#embed-object-embed-field-structure
|
||||
*/
|
||||
interface DiscordEmbedField {
|
||||
name: string;
|
||||
value: string;
|
||||
inline?: boolean;
|
||||
}
|
||||
|
||||
export async function reportListingToDiscord(
|
||||
listing: MarketplaceListing,
|
||||
reportingProfile: string,
|
||||
): Promise<any> {
|
||||
if (!DISCORD_TOKEN || !DISCORD_CHANNEL_ID) {
|
||||
interface DiscordEmbed {
|
||||
title: string;
|
||||
color: number;
|
||||
description: string;
|
||||
timestamp: string;
|
||||
fields: DiscordEmbedField[];
|
||||
}
|
||||
|
||||
interface DiscordMessage {
|
||||
username: string;
|
||||
embeds: DiscordEmbed[];
|
||||
}
|
||||
|
||||
const postToWebhook = async (content: DiscordMessage): Promise<AxiosResponse | void> => {
|
||||
// If convar isnt set
|
||||
if (!DISCORD_WEBHOOK) {
|
||||
discordLogger.warn(
|
||||
'Got a request to report a listing but discord is not configures. See README o how to configure discord endpoints.',
|
||||
);
|
||||
return;
|
||||
}
|
||||
const resp = await axios.post(DISCORD_WEBHOOK, { ...content });
|
||||
// If we get a bad request throw error
|
||||
if (resp.status >= 200 && resp.status < 300)
|
||||
throw new Error(`Discord Error: ${resp.status} Error - ${resp.statusText}`);
|
||||
};
|
||||
|
||||
const content = `**REPORTED LISTING**
|
||||
-------------------------------------------------
|
||||
**Reporting User**: ${reportingProfile}
|
||||
**Reported User** ${listing.name} (${listing.username})
|
||||
-------------------------------------------------
|
||||
**Reported Listing**
|
||||
${listing.title}
|
||||
----
|
||||
${listing.description}
|
||||
`;
|
||||
const createDiscordMsgObj = (type: string, message: string, fields: DiscordEmbedField[]) => {
|
||||
// Get ISO 8601 as its required by Discord API
|
||||
const curTime = new Date().toISOString();
|
||||
|
||||
return await axios.post(POST_CHANNEL_URL, { content }, { headers: DISCORD_HEADERS });
|
||||
return {
|
||||
username: 'NPWD Report',
|
||||
embeds: [
|
||||
{
|
||||
title: `${type} REPORT`,
|
||||
color: 0xe74c3c,
|
||||
description: message,
|
||||
timestamp: curTime,
|
||||
fields,
|
||||
},
|
||||
],
|
||||
};
|
||||
};
|
||||
|
||||
export async function reportTweetToDiscord(tweet: Tweet, reportingProfile: Profile): Promise<any> {
|
||||
// TODO: Add image report functionality
|
||||
const fields = [
|
||||
{
|
||||
name: 'Reporting User',
|
||||
value: `${tweet.profile_name} (${tweet.profile_id})`,
|
||||
},
|
||||
{
|
||||
name: 'Reported User',
|
||||
value: `${tweet.profile_name} (${tweet.profile_id})`,
|
||||
},
|
||||
{
|
||||
name: 'Tweet',
|
||||
value: tweet.message,
|
||||
},
|
||||
];
|
||||
|
||||
// If the tweet has images add it to the field array
|
||||
if (tweet.images)
|
||||
fields.concat({
|
||||
name: 'Images',
|
||||
value: tweet.images.split(IMAGE_DELIMITER).join('\n'),
|
||||
});
|
||||
|
||||
const msgObj = createDiscordMsgObj('TWITTER', `Received a report for a tweet`, fields);
|
||||
try {
|
||||
await postToWebhook(msgObj);
|
||||
} catch (e) {
|
||||
discordLogger.error(e.message);
|
||||
}
|
||||
}
|
||||
|
||||
export async function reportListingToDiscord(
|
||||
listing: MarketplaceListing,
|
||||
reportingProfile: string,
|
||||
): Promise<void> {
|
||||
const embedObj = createDiscordMsgObj('MARKETPLACE', 'Received a report for a listing', [
|
||||
{
|
||||
name: 'Reported By',
|
||||
value: reportingProfile,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: 'Reported User',
|
||||
value: `${listing.name} - (${listing.username})`,
|
||||
inline: true,
|
||||
},
|
||||
{
|
||||
name: 'Reported Listing Title',
|
||||
value: listing.title,
|
||||
},
|
||||
{
|
||||
name: 'Reported Listing Desc.',
|
||||
value: listing.description,
|
||||
},
|
||||
]);
|
||||
|
||||
try {
|
||||
await postToWebhook(embedObj);
|
||||
} catch (e) {
|
||||
discordLogger.error(e.message);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user