Sign in to follow this  
Captien

discord [TUTORIAL] Sending Server logs to Discords

Recommended Posts

it's always useful to keep an eye on the activity of the server like players joining/leaving the server while you're chatting on discord. This can be achieved by using our lovely Webhook friends. This tutorial aims to give you knowledge about how to use those web-hooks to keep in contact with some server logs.

Creating a Webhook

It's really easy, create a channel go to channel settings -> webhooks -> Create a webhook

I'll call mine Spicy.

 

8cS_QRQkS-ec16p2b5Ng-A.png

Copy the webhook URL and wait for further instructions.

Using the Webhook

Now we'll create a small server-side function that'll help us use the webhook.

Spoiler
const webhook = 'Insert webhook URL';
const serverLogo = 'Shiny logo URL'
const got = require('got'); // Fast beast request package

mp.discord = {}; // Global discord object bound to mp
mp.discord.colors = { // Nice colors to change the embed's color
    RED: 13632027,
    GREEN: 4289797,
    BLUE: 4886754,
    ORANGE: 16098851,
    BLACK: 1,
    WHITE: 16777215,
    GREY: 10197915,
    YELLOW: 16312092,
    BROWN: 9131818,
    CYAN: 5301186
};
/*
* @function Our nice function send some logs
* @params:
* title: string (Title of the embed)
* msg: string (description of the log)
* fields: array of objects optional {name: string, value: string, inline: boolean}
* color: int optional (Default: GREY color) use mp.discord.colors.color for more colors
*/
mp.discord.sendMessage = (title, msg, fields = [], color = mp.discord.colors.GREY) => {
    const embed = {
        "embeds": [{
            "author": {
                "name": mp.config.name, // Server's name
                "icon_url": serverLogo // Server's logo
            },
            "title": title,
            "description": msg,
            "thumbnail": {
                "url": serverLogo
            },
            "fields": fields,
            "color": color
        }]
    };
    got.post(webhook, { // Method to post the log into discord webhook
        body: JSON.stringify(embed),
        headers: {
            'content-type': "application/json"
        }
    })
};

PS: Don't forget to do npm install got --save  before using this package.

Now here's a small example of how to use it:
 

// Triggers once player joins the server...

mp.events.add('playerJoin', (player) => {
	mp.discord.sendMessage(`Player joined the server [${mp.players.toArray().length}/${mp.config.maxplayers}]`, player.name, [], mp.discord.colors.GREEN);
});

// Another one with fields

mp.events.add('playerJoin', (player) => {
  let fields = [{
    name: `Player's IP`,
    value: player.ip,
    inline: false
  }]
  mp.discord.sendMessage(`Player joined the server [${mp.players.toArray().length}/${mp.config.maxplayers}]`, player.name, fields, mp.discord.colors.GREEN);
});

BTKl7cDJSAqp0hVwBy7J5g.png

upmBrpilRK637rtjZpXyMA.png

 

Now you have your own discord logger, use it wisely. Enjoy spamming discord....

 

Regards,
Keptin

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.