Discord Chat Relay

Share scripts you wrote for Denizen-Bukkit here!
Post Reply
User avatar
bear
New
New
Posts: 1
Joined: January 26th, 2019, 2:39 pm

Discord Chat Relay

Post by bear » March 2nd, 2019, 12:34 pm

Connects your discord bot to your Minecraft Server, and relays the chat two-ways as well as player events to your discord channel of choice. This script requires the plugin(s) dDiscordBot found here: https://ci.citizensnpcs.co/job/dDiscordBot/

Preview:
Spoiler!
Image
1 )drop main script into denizen/scripts/
2a) drop config file into denizen/data/Discord_Chat_Relay_Config.yml [make sure it's named this as well]
2b) if this directory doesn't exist, create it
3 ) plug your bot token & channel ID into Discord_Chat_Relay_Config.yml
4 ) restart server or reload scripts, followed by issuing the /discord reload -a command

This script has Message Constructor Library compatibility; if you don't already use this script, it's awesome and you should pick it up! Elsewise you'll just be missing out on click-able chat text. Works fine without.

This script also has Votifier compatibility; if you want notifications in discord chat of votes, leave it as is, elsewise delete these lines (found on line 103):
Spoiler!

Code: Select all

# ███ [ Discord Player Events | Addons  ] ███
      on votifier vote:
        - discord id:discord_chat_relay message channel:<server.flag[discord.mainchat]> "<&co>diamond_shape_with_a_dot_inside<&co> **<context.username>** voted!"
Note: This event requires the plugin(s) DepenizenBukkit, Votifier.

Here are the two scripts in a link you'll need Separately:
*Click Here for the main script
*Click Here for the configuration script

Additionally, they can be copied here:
Spoiler!
Main Script:

Code: Select all

# ███████████████████████████████████████████████████████████
# ██ [           dDiscord Chat Relay                     ] ██
# ██ [                                                   ] ██
# ██ [   This script contains MCL compatibility;         ] ██
# ██ [   to take full advantage of some neat             ] ██
# ██ [   features, consider also importing the           ] ██
# ██ [   Message Constructor Library!                    ] ██
# ██ [                                                   ] ██
# ██ [   The config for this file should be              ] ██
# ██ [   SEPARATE from this script; you do               ] ██
# ██ [   NOT want to keep your bot token in ram,         ] ██
# ██ [   keep it separate. Anyone with your              ] ██
# ██ [   token has full access to your bot.              ] ██
# ██ [                                                   ] ██
# ██ [   Step one: Create a discord bot. If you          ] ██
# ██ [     need further instructions, you should         ] ██
# ██ [     seek a guide online.                          ] ██
# ██ [                                                   ] ██
# ██ [   You need to obtain # ID's:                      ] ██
# ██ [   * Your Bot Token. Can be obtained here:         ] ██
# ██ [   https://discordapp.com/developers/applications/ ] ██
# ██ [   * Your Discord Channel ID's.                    ] ██
# ██ [   Right click the channel > Copy ID               ] ██
# ██ [                                                   ] ██
# ██ [   Create a directory in your denizen folder:      ] ██
# ██ [   * /plugins/denizen/Data/                        ] ██
# ██ [                                                   ] ██
# ██ [   Create the external file inside:                ] ██
# ██ [   * Discord_Chat_Relay_Config.yml                 ] ██
# ██ [                                                   ] ██
# ██ [   Paste the bot's information                     ] ██
# ██ [                                                   ] ██
# ███████████████████████████████████████████████████████████

# ███ [ Discord chat format             ] ███
DiscordRelayChatFormat:
  type: format
  debug: false
  format: "<&2><&l>[<&r><&a>Discord<&4><&l>]<&r><&f><text>"

# ███ [ DiscordBot | Discord Control    ] ███
Discord_Relay_Chat_Handler:
    type: world
    debug: False
    events:

# ███ [ Server Startup                  ] ███
      on server start:
        - wait 10s
        - run Discord_Chat_Relay_Config instantly "context:-a"
        - wait 1s
        - discord id:discord_chat_relay message channel:<server.flag[discord.mainchat]> "<&co>white_check_mark<&co> **Server has started**"

# ███ [ Ingame Chat Relay               ] ███
      on player chats:
        - discord id:discord_chat_relay channel:<server.flag[discord.mainchat]> message "**<player.groups.get[1]>** <player.name.display.strip_color>: <context.message.strip_color>"

# ███ [ Discord Chat Relay              ] ███
      on discord message received by discord_chat_relay:
        - if <context.channel> == <server.flag[discord.mainchat]> && <context.message> != "" && <context.author_id> != <context.self>:
          - announce format:DiscordRelayChatFormat " | <context.author_name><&3><&l>]<&r> <&8>»<&f> <context.message.parse_color>"

# ███ [ Discord Player Events           ] ███
      on player first logs in:
        - discord id:discord_chat_relay message channel:<server.flag[discord.mainchat]> "<&co>confetti_ball<&co> **<player.name> has logged in for the first time. Welcome them!** <&co>confetti_ball<&co>"
      on player joins:
        - discord id:discord_chat_relay message channel:<server.flag[discord.mainchat]> "<&co>heavy_plus_sign<&co> **<player.name.display.strip_color> has logged in.**"
      on player quits:
        - discord id:discord_chat_relay message channel:<server.flag[discord.mainchat]> "<&co>heavy_multiplication_x<&co> **<player.name.display.strip_color> has left the server.**"
      on player dies:
        - discord id:discord_chat_relay message channel:<server.flag[discord.mainchat]> "<&co>skull<&co> **<context.message>**"
      on shutdown:
        - discord id:discord_chat_relay message channel:<server.flag[discord.mainchat]> "<&co>octagonal_sign<&co> **Server is restarting - Please wait**"
        - flag server discord.botstatus:!

# ███ [ Discord Player Events | Addons  ] ███
      on votifier vote:
        - discord id:discord_chat_relay message channel:<server.flag[discord.mainchat]> "<&co>diamond_shape_with_a_dot_inside<&co> **<context.username>** voted!"

# ███ [ Discord Channel Command/Update  ] ███
Discord_Link_And_Configuration_Command:
  type: command
  name: discord
  debug: false
  description: Sends you the discord join link
  permission: discord.command
  permission message: "<proc[msgHover].context[<&c>You do not have permission.|<script.yaml_key[permission]>]||<&c>You do not have permission.>"
  usage: /discord
  advanced usage: "<proc[msgHint].context[<&6>/discord|discord|<&a>Click to enter Link command]||<&6>/discord>"
  aliases:
    - disc
  syntax message:
    - if <player.is_op>:
      - define mh2 "<proc[msgHint].context[(<&e>reload<&6>)|discord reload|<&a>Click to enter Reload command]||(<&e>reload<&6>)>"
      - define mh3 "<proc[msgHint].context[(<&e>-a<&6>)|discord reload -a|<&a>Click to enter Reload All command]||(<&e>-a<&6>)> <&pipe> <&f><parse:<script.yaml_key[description]>>"
      - narrate format:DiscordRelayChatFormat "<&e>Syntax<&co> <parse:<script.yaml_key[advanced usage]>> <def[mh2]> <def[mh3]> as well as reloads the discord configuration, optionally including the discord bot token."
    - else:
      - narrate format:DiscordRelayChatFormat "<&e>Syntax<&co> <parse:<script.yaml_key[advanced usage]>> <&pipe> <&f><script.yaml_key[description]>."
  script:
    - if <context.args.size> == 0 || <context.args.get[1]||null> == "link":
      - narrate format:DiscordRelayChatFormat "Discord Link<&co> <&e><proc[msgUrl].context[Click Here for Link|<server.flag[discord.link]>|<&a><server.flag[discord.link]>]||<server.flag[discord.link]>>"
      - queue clear
    - else if <context.args.size.is[MORE].than[1]> && <player.is_op.not>:
      - inject locally "syntax message" instantly
      - queue clear
    - else if <contxt.args.get[2]||null> == "-a" || <contxt.args.get[2]||null> == "null":
      - if <context.args.get[1]> == "reload":
       - run Discord_Chat_Relay_Config instantly context:<context.args.get[2]||null>
       - queue clear
      - inject locally "syntax message" instantly
      - queue clear
    - else:
      - inject locallly "syntax message" instantly

# ███ [ DiscordBot | Configuration      ] ███
Discord_Chat_Relay_Config:
  type: task
  debug: false
  definitions: Arg2
  script:
    - yaml load:data/Discord_Chat_Relay_Config.yml id:discord_chat_relay_temp
    - flag server discord.key:loaded
    - if <def[Arg2]> == "-a":
      - if <server.flag[discord.botstatus]> == "running":
        - flag server discord.botstatus:!
        - discord id:discord_chat_relay disconnect
      - discord id:discord_chat_relay connect code:<yaml[discord_chat_relay_temp].read[bots.discord.botToken]>
      - flag server discord.botstatus:running
    - flag server discord.link:<yaml[discord_chat_relay_temp].read[bots.discord.discordlink>
    - flag server discord.mainchat:!
    - flag server discord.mainchat:<yaml[discord_chat_relay_temp].read[bots.discord.channel.mainchat]>
    - yaml unload id:discord_chat_relay_temp
    - flag server discord.key:!
Configuration Script:

Code: Select all

# ████████████████████████████████████████████████████
# ██ [           dDiscord Chat Relay              ] ██
# ██ [                                            ] ██
# ██ [   1] your bot token is placed in:          ] ██
# ██ [      [botToken: ]                          ] ██
# ██ [                                            ] ██
# ██ [   2] your channel ID is placed in:         ] ██
# ██ [      [mainchat: ]                          ] ██
# ██ [                                            ] ██
# ██ [   3] your discord link is placed in:       ] ██
# ██ [      [discordlink: ]                       ] ██
# ██ [                                            ] ██
# ██ [   4] your paypal/bank account information  ] ██
# ██ [      is placed in:                         ] ██
# ██ [      [the link through DonateLink]         ] ██
# ██ [                                            ] ██
# ████████████████████████████████████████████████████
bots:
  discord:
    botToken: '69.69.666.This.Is.Your.Bot.Token.666.69.69'
    channel:
      mainchat: '123456789012345678'
    discordlink: 'https://discord.gg/4beFHHv'
    DonateLink: 'https://one.denizenscript.com/donate'
Things to note:
  • You need to have your own bot, or at least a token to a bot with access to the discord channel you wish to relay chat to.
  • Your bot must have permission to send messages in the channel you wish to relay chat to.
  • you NEED to separate these files; never keep your discord token in ram. This second file is configured to be saved in as well as named:
/plugins/denizen/data/Discord_Chat_Relay_Config.yml
I'm notorious for tiny mistakes; so if there's an error, let me know and i'll fix it as quickly as i can. This is the script I use for my own server, minus a bit of logic removed that I didn't think should be included like my staff-chat and build-chat.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest