Skip to content

Instantly share code, notes, and snippets.

@druv5319
Created August 8, 2022 19:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save druv5319/297b409a0569a8588ce6de45c7867885 to your computer and use it in GitHub Desktop.
Save druv5319/297b409a0569a8588ce6de45c7867885 to your computer and use it in GitHub Desktop.
Send and Receive SMS Messages via Discord with Twilio and Node.js
require('dotenv').config();
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildPresences, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, , GatewayIntentBits.GuildMembers] });
const twilio = require('twilio')(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
const friendsChannelId = 'XXXXXX';
const myUserId = 'XXXXXX';
const express = require('express')
const app = express();
app.use(express.urlencoded({
extended: true
}));
function sendSMS(sms) {
twilio.messages.create({
body: sms,
to: process.env.PERSONAL_NUMBER,
from: process.env.TWILIO_NUMBER,
});
}
client.on("messageCreate", function(message) {
if (message.author.bot) return;
if (message.channel.id == friendsChannelId && message.author.id != myUserId) {
const sms = `${message.author.username}: ${message.content}`;
sendSMS(sms);
}
else if(message.mentions.members.has(myUserId)) {
const sms = `${message.author.username} mentioned you in ${message.channel.name}: ${message.content}`
sendSMS(sms);
}
});
client.on("guildMemberAdd", function(member) {
sendSMS(`${member.user.username} has joined the server!`)
});
client.on("guildMemberRemove", function(member) {
sendSMS(`${member.user.username} has left the server!`)
});
app.post('/sms', async (req, res) => {
if(req.body.From != process.env.PERSONAL_NUMBER) return;
const channel = client.channels.cache.get(friendsChannelId);
channel.send(req.body.Body)
});
app.listen(3000, () => {
console.log(`Listening on port 3000`);
});
client.login(process.env.BOT_TOKEN);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment