PsychoTea / Oxide.Ext.Discord

Discord extension and integration
https://umod.org/extensions/discord
12 stars 25 forks source link

Oxide.Ext.Discord is an Oxide extension which acts as a bridge between Oxide and the Discord API.

Should you encounter a problem or bug with the extension, please feel free to create an issue here. Try to include as much detail as possible, including steps to reproduce the issue. A code example is highly appreciated.

Installation

To install the extension to your Oxide server, you must follow a few simple steps: 1) Shutdown the server. 2) Open the server files, and navigate to the "Managed" folder (eg. "RustDedicated/Managed") 3) Download the latest release. 4) Unzip the release. 5) Copy the "Oxide.Ext.Discord.dll" file into your "Managed" folder. 6) Start your server!

Getting your API Key

An API key is used to authenticate requests made to and from Discord.

Note: DO NOT SHARE YOUR API KEY! Sharing your key may result in punishments from Discord (including a platform-wide ban) if the token is used to abuse the API.

Obtaining an API Key: 1) Visit the official Discord Developers page here: Discord Developer Documenation 2) Click "New App". 3) Name your app and click create! Note: A description isn't required. 4) You will now be redirected to your created app, at the point you will need to click "Create a Bot User". 5) Under the newly created bot section, under "Username" you will see "Token". Reveal the token and copy it into your plugin. 6) Now it's time to add your new bot to your guild! To add your bot to your guild you must visit the following link, and replace "botUserID" with the client ID found at the top of your Discord app settings page: https://discordapp.com/oauth2/authorize?client_id=botUserID&scope=bot&permissions=8

Note: "permissions=8" in the link will provide the bot with administrative permissions so you won't have to give it some.

Plugin Example

The following plugin is a simple example on how to use the extension. This plugin simply adds a 'sad' reaction to any message posted on the Discord server, and then broadcasts the message to the server chat.

using Oxide.Ext.Discord;
using Oxide.Ext.Discord.Attributes;
using Oxide.Ext.Discord.DiscordObjects;

namespace Oxide.Plugins
{
    [Info("Discord Ext Example", "Your Name", "1.0.0")]
    class DiscordExtExample : CovalencePlugin
    {
        // Define the DiscordClient field that will be set
        // to when our DiscordClient is created & connected
        [DiscordClient] DiscordClient Client;

        void OnServerInitialized()
        {
            Discord.CreateClient(this, "<api key here>"); // Create a new DiscordClient
        }

        // Called when the connection is completed
        void Discord_Ready(Ready ready)
        {
            Puts("Discord connected!");

            // When this hook is called, our Client variable
            // will be set to the main DiscordClient that
            // has been created for us

            Puts($"Connected to server: {Client.DiscordServer.name}");
        }

        // Called when a message is created on the Discord server
        void Discord_MessageCreate(Message message)
        {
            // Add a sad reaction to the message
            message.CreateReaction(Client, ":sad:");

            // Post the message to chat
            server.Broadcast($"Discord Message: {message.author.username} - {message.content}");
        }
    }
}

Socket Hooks

DiscordSocket_Initalized

void DiscordSocket_Initalized(DiscordClient client)
{
    Puts("Client Initalized!");
}

DiscordSocket_HeartbeatSent

void DiscordSocket_HeartbeatSent()
{
    Puts("Heartbeat sent to discord!");
}

DiscordSocket_WebSocketOpened

void DiscordSocket_WebSocketOpened()
{
    Puts("WebSocket Opened!");
}

DiscordSocket_WebSocketClosed

void DiscordSocket_WebSocketClosed(string reason, int code, bool clean)
{
    Puts("WebSocket closed!");
}

DiscordSocket_WebSocketErrored

void DiscordSocket_WebSocketErrored(Exception exception, string message)
{
    Puts($"WebSocket errored:.");
}

Discord API Event Hooks

Discord_Ready

void Discord_Ready(Ready ready)
{
    Puts("Discord is ready!");
}

Discord_Resumed

void Discord_Resumed(Resumed resumed)
{
    Puts("Discord Connection Resumed!");
}

Discord_ChannelCreate

void Discord_ChannelCreated(Channel channel)
{
    Puts("Discord Channel Created");
}

Discord_ChannelUpdate

void Discord_ChannelUpdate(Channel updatedChannel, Channel oldChannel)
{
    Puts("Discord Channel Updated");
}

Discord_ChannelDelete

void Discord_ChannelDelete(Channel channel)
{
    Puts("Discord Channel Deleted!");
}

Discord_ChannelPinsUpdate

void Discord_ChannelPinsUpdate(ChannelPinsUpdate update)
{
    Puts("The pins on a channel have been updated!");
}

Discord_GuildCreate

void Discord_GuildCreate(Guild guild)
{
    Puts("A guild has been created!");
}

Discord_GuildUpdate

void Discord_GuildUpdate(Guild guild)
{
    Puts("A guild has been updated!");
}

Discord_GuildDelete

void Discord_GuildDelete(Guild guild)
{
    Puts("A guild has been deleted!");
}

Discord_GuildBanAdd

void Discord_GuildBanAdd(User user)
{
    Puts("A user has been banned!");
}

Discord_GuildBanRemove

void Discord_GuildBanRemove(User user)
{
    Puts("A user has been unbanned!");
}

Discord_GuildEmojisUpdate

void Discord_GuildEmojisUpdate(GuildEmojisUpdate update)
{
    Puts("The emoji's have been updated!");
}

Discord_GuildIntergrationsUpdate

void Discord_GuildIntergrationsUpdate(GuildIntergrationsUpdate update)
{
    Puts("Guild Intergrations Updated!");
}

Discord_MemberAdded

void Discord_MemberAdded(GuildMember member)
{
    Puts("A user has been added to the server!");
}

Discord_MemberRemoved

void Discord_MemberRemoved(GuildMember member)
{
    Puts("A user has been removed from the server!");
}

Discord_GuildMemberUpdate

void Discord_GuildMemberUpdate(GuildMemberUpdate update, GuildMember oldMember)
{
    Puts("A guild member has been updated!");
}

Discord_GuildMembersChunk

void Discord_GuildMembersChunk(GuildMembersChunk chunk)
{
    Puts("A guild members chunk has been recieved!")
}

Discord_GuildRoleCreate

void Discord_GuildRoleCreate(Role role)
{
    Puts("A new role has been created!");
}

Discord_GuildRoleUpdate

void Discord_GuildRoleUpdate(Role newRole, Role oldRole)
{
    Puts("A role has been updated!");
}

Discord_GuildRoleDelete

void Discord_GuildRoleDelete(Role role)
{
    Puts("A role has been deleted!");
}

Discord_MessageCreate

void Discord_MessageCreate(Message message)
{
    Puts("A new message has been created!");
}

Discord_MessageUpdate

void Discord_MessageUpdate(Message message)
{
    Puts("A message has been updated!");
}

Discord_MessageDelete

void Discord_MessageDelete(MessageDelete message)
{
    Puts("A message has been deleted!");
}

Discord_MessageDeleteBulk

void Discord_MessageDeleteBulk(MessageDeleteBulk bulk)
{
    Puts("A bulk of messages have been deleted!");
}

Discord_MessageReactionAdd

void Discord_MessageReactionAdd(MessageReactionUpdate update)
{
    Puts("A reaction has been added to a message!");
}

Discord_MessageReactionRemove

void Discord_MessageReactionRemove(MessageReactionUpdate update)
{
    Puts("A reaction has been removed from a message!");
}

Discord_ReactionRemoveAll

void Discord_MessageReactionRemoveAll(MessageReactionRemoveAll reactions)
{
    Puts("All reactions have been removed from a message!");
}

Discord_PresenceUpdate

void Discord_PresenceUpdate(PResenceUpdate update)
{
    Puts("Someone's presence has been updated!");
}

Discord_TypingStart

void Discord_TypingStart(TypingStart start)
{
    Puts("Someone has started typing!");
}

Discord_UserUpdate

void Discord_UserUpdate(User user)
{
    Puts("A user has been updated!");
}

Discord_VoiceStateUpdate

void Discord_VoiceStateUpdate(VoiceState state)
{
    Puts("A users voice state has been updated!");
}

Discord_VoiceServerUpdate

void Discord_VoiceServerUpdate(VoiceServerUpdate update)
{
    Puts("The voice server has been updated!");
}

Discord_WebhooksUpdate

void Discord_WebhooksUpdate(WebhooksUpdate webhooks)
{
    Puts("The webhooks have been updated!");
}

Discord_UnhandledEvent

void Discord_UnhandledEvent(JObject messageObject)
{
    Puts("An unhandlded event has occured!");
}

Contributing

Want to contribute? Create a fork of the repo and create a pull request for any changes you wish to make!