SHARE
TWEET

Untitled

a guest Jan 20th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Discord = require('discord.js');
  2. const striptags = require('striptags');
  3.  
  4. const ExpressionEngineScript = require('./ee');
  5.  
  6. // check out this page for instructions on getting a channelId and loginToken
  7. // https://github.com/Chikachi/DiscordIntegration/wiki/How-to-get-a-token-and-channel-ID-for-Discord
  8. // https://discordapp.com/developers/applications/me
  9.  
  10.  
  11. // edit this config to use your own discord info / character name
  12. const config = {
  13.     loginToken: "your token for your discord bot", // change this to the login token for discord bot
  14.     channel: "535267517971562516", // change this to your own channel id
  15.     characterName: "Gdiscord" // change this to the AC character name running this script
  16. };
  17.  
  18. // Create an instance of a Discord client
  19. const discord = new Discord.Client();
  20.  
  21. // create an instance of an ee script
  22. // note: this is required unless you really know what you are doing.
  23. const ee = new ExpressionEngineScript();
  24.  
  25. // handle discord ready/connected event
  26. discord.on('ready', () => {
  27.     ee.log('connected to discord');
  28. });
  29.  
  30. // handle messages from the AC client so we can forward to discord
  31. ee.on("ChatBoxMessage", (event) => {
  32.     // we only want allegiance messages
  33.     if (event.text.startsWith("[Allegiance]")) {
  34.         // find the discord channel defined in our config
  35.         const channel = discord.channels.find(ch => ch.id === config.channel);
  36.  
  37.         // remove the [Allegiance] prefix from the message
  38.         let newMessage = event.text.replace("[Allegiance] ", "");
  39.  
  40.         // if this is a message from the character running the bot, bail out
  41.         if (newMessage.startsWith(config.characterName + " says")) return;
  42.  
  43.         // send our modified message to the discord channel if it exists
  44.         if (channel) channel.send(newMessage);
  45.     }
  46. });
  47.  
  48. // Create an event listener for discord messages so we can forward to AC
  49. discord.on('message', message => {
  50.     // if this didnt happen in the channel we are monitoring bail out
  51.     if (message.channel.id !== config.channel) return;
  52.  
  53.     // if this is a bot dont dont forward the message
  54.     if (message.author.bot) return;
  55.  
  56.     // this is the discord username of the person who posted the message
  57.     let username = message.author.username;
  58.  
  59.     // get the message contents, but remove any newline chars
  60.     let newMessage = message.content.replace(/(\r|\n)/gm, "");
  61.  
  62.     // if the user has a nickname set, use that instead
  63.     if (message.member && message.member.nickname) {
  64.         username = message.member.nickname;
  65.     }
  66.  
  67.     // strip out any html-like tags from the message
  68.     newMessage = striptags(newMessage);
  69.  
  70.     // if the message is empty now we shouldn't forward it
  71.     if (newMessage.length == 0) return;
  72.  
  73.     // format the message and limit to 180 chars
  74.     newMessage = `${username} says, ${newMessage}`.substr(0, 180);
  75.  
  76.     // send this to the chat parser of ac client running this script.
  77.     // this acts like the client typed the message into the chat box.
  78.     ee.invokeChatParser("/a " + newMessage);
  79. });
  80.  
  81. // Log in our discord bot
  82. discord.login(config.loginToken);
  83.  
  84. // scripts *must* return ee.register()
  85. return ee.register();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top