Skip to content

Instantly share code, notes, and snippets.

@heathermhargreaves
Created October 26, 2021 17:57
Show Gist options
  • Save heathermhargreaves/6f7d5c9966013cca7a8a3b790f1707a1 to your computer and use it in GitHub Desktop.
Save heathermhargreaves/6f7d5c9966013cca7a8a3b790f1707a1 to your computer and use it in GitHub Desktop.
const express = require('express')
const app = express()
const port = 3000
require('dotenv').config()
const _ = require('lodash')
const Analytics = require('analytics-node');
const analytics = new Analytics(process.env.SEGMENT_KEY);
//////////////////////////
////// User Profile //////
//////////////////////////
let discord_id
let discord_text
let discord_handle
let user = {
name: 'heather',
email: 'hhargreaves@twilio.com',
discord_handle: discord_handle || 'gobears2013',
userid: discord_id || '2342ksdaf'
}
//////////////////////////
// Watson Tone Analyzer //
//////////////////////////
const ToneAnalyzerV3 = require('ibm-watson/tone-analyzer/v3');
const { IamAuthenticator } = require('ibm-watson/auth');
const toneAnalyzer = new ToneAnalyzerV3({
version: '2021-10-24',
authenticator: new IamAuthenticator({
apikey: process.env.WATSON_KEY,
}),
serviceUrl: 'https://api.au-syd.tone-analyzer.watson.cloud.ibm.com/instances/dd6d1ccd-ac09-4072-ac17-a8963052f09b',
});
//////////////////////////
///////// Disord /////////
//////////////////////////
// Require the necessary discord.js classes
const { Client, Intents } = require('discord.js');
require('dotenv').config();
var userMap = new Map();
// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.DIRECT_MESSAGES], partials: ['MESSAGE', 'CHANNEL']
});
// const text = discord_text
let text
// When the client is ready, run this code (only once)
client.once('ready', () => {
console.log('Ready!');
});
// const get_discord_user = async () => {
client.on("messageCreate", async function(message){
console.log("this is the message", message);
discord_id = message.author.id
discord_handle = message.author.username
text = message.content
console.log('this is the text', text)
// const discord_user = client.users.cache.get(discord_id);
// // let discord_email = client.user.email
// // console.log('this is the discord user', discord_user)
// // console.log('this is the email', discord_email)
const toneParams = {
toneInput: { 'text': text },
contentType: 'application/json',
};
let sentiment_result;
const get_sentiment_result = async () => toneAnalyzer.tone(toneParams)
.then(toneAnalysis => {
let tonesArr = toneAnalysis.result.document_tone.tones
console.log('tones analysis', toneAnalysis.result)
let sentiment = _.sortBy(tonesArr, ['tone_name', 'score'])
sentiment_result = sentiment[0].tone_name
console.log(sentiment, sentiment_result);
return sentiment_result
})
.catch(err => {
console.log('error:', err);
});
async function identify_segment() {
console.log('this is the user id', user)
analytics.identify({
userId: discord_id,
traits: {
name: user.name,
email: user.email,
discord_handle: discord_handle
}
});
}
async function track_segment_event() {
const result = await get_sentiment_result();
analytics.track({
userId: discord_id,
event: 'Contacted Discord Support',
properties: {
sentiment: result || "no sentiment recorded",
}
});
console.log('this is the sentiment', result)
}
get_sentiment_result()
identify_segment()
track_segment_event()
let author = message.author;
if (author.id == '902273325076717588') {
return;
}
userMap.set(author.id)
if (message.channel.type == 'GUILD_TEXT' && message.content.toLowerCase().includes('help')) {
message.author.send();
}
else if (message.channel.type == 'DM') {
message.author.send("yes!");
}
return message
})
const getUserByID = async (id) => {
const user = await client.users.fetch(`${id}`);
console.log(user)
return user;
}
// }
// Login to Discord with your client's token
client.login(process.env.DISCORD_KEY);
//////////////////////////
//////// Segment /////////
//////////////////////////
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment