Code Monkey home page Code Monkey logo

nebucord's Introduction

Nebucord - Discord WebSocket and REST API

GitHub Release GitHub Development GitHub license Security Rating Quality Gate Status

SonarCloud

This is another implementation of the Discord API. It implements an HTTP WebSocket client as well as a REST API.
An actual in use sample can be found on our Discord server: Nebulatien Discord

If you need help or have questions, don't hestiate to contact Me. Best done if you join the above mentioned Discord server or (if you found something) create an issue in Github. Also pull request for fixes of course are welcome.

Of course not fully finished is it still able to do the most important things. This includes:

  • Nebucord WebSocket API

    • Setup and run bot(s)
    • Set status for bots
    • Simple control commands
    • Callback classes to intercept Discord gateway events
    • Changing default internal command behaviours
    • Setting user id's for bot controlling
    • Configurable by parameter or by .ini file
    • Customable gateway intents
    • Application and interactions API
  • Nebucord REST API

    • Model oriented interface on data receiving
    • Application and interactions API
    • Followup Messages for interactions
    • Complete Discord REST API
    • Configurable by parameter or by .ini file
  • What's missing

    • Many error checking
    • Way more better logging and debugging options
    • Multithreading (if applicable, thanks to bitnykk for the idea)

Default and available parameters

Parameter Config name INI config name Default value
Bot token token bottoken (string)empty
ACL user snowflakes ctrluser acl (array)[]
WS connection retries wsretries websocket.retries (integer)3
Default GW intent bitmask* intents intents.* (integer)3276541 / (boolean)true*
DM on failures dmonfailures websocket.dmonfailures (boolean)true

*Note:
The intent bitmask defaults to be everything is true except GUILD_MEMBERS, GUILD_PRESENCES, GUILD_MESSAGES and MESSAGE_CONTENT. These four options has to be manual set to true and they have to be activated in the Bot preferences on the Discord application management webpage. The bitmask is 3276799 if everything set to true or for config as parameter.

Note:
Configuration by parameter has priority to configuration by .ini file.

Requirements

Additional Composer packages are required in order to run Nebucord.
Of course these can also be installed without Composer, but you have to include the SPL autoloader of the packages as well.

The recommended way is to use Composer.

Package informations:

Install

By composer:

user@linux:~# composer require eurobertics/nebucord

By Docker (more information can be found on Nebucord Docker Hub):

user@linux:~# docker pull eurobertics/nebucord:latest

By GitHub Packages (more information can be found on GitHub Pulling container images):

docker pull ghcr.io/eurobertics/nebucord:latest

Note:
If you want use .ini configuration, you have to put your .ini File (nebucord.ini i. e.) in the mounted directory for your Docker instance and set the path to the path of the docker working directory. You can find more information about this on the Nebucord Docker Hub Page.


By GIT:
Simple clone this repository and use the native autoloader file in ./src Directory.

user@linux:~# git clone https://github.com/Eurobertics/Nebucord.git

Note: If you use the library without composer, you just can include the native autoloader:

include "src/nebucordAutoloader.php";

Note: You have to include the native autoloader of the dependencie packages as well. See package dependencies above.


Example usage - WebSocket API

The WebSocket API is designed to run as a PHP CLI program.

Usage websocket API, minimalistic example:

<?php
include "vendor/autoload.php"; // Composer autloader

use Nebucord\Nebucord;

$nebucord = new Nebucord(['token' => 'your_bot_token', 'ctrlusr' => ['controluser-snowflake1', 'controluser-snowflake2']]);
$nebucord->bootstrap()->run();

'your_bot_token': The auth token of your bot (from the Discord Dev-Portal)

'controluser-snowflake': The snowflake of Discord user(s) who can issue control commands such like 'shutdown' to the bot.

Of course this only starts the bot and this one sits there and does nothing.

A more complex example:

<?php
include "vendor/autoload.php"; // Composer autoloader

use Nebucord\Nebucord;

class MessageInterceptorClass {
    public function onMessageReceive($evt) {
        // $evt is a model with all data from the gateway if a message create
        // event is received
        
        echo $evt->content; // returns the message
    }
}

$nebucordEventTable = \Nebucord\Events\EventTable::create();
$nebucordEventTable->addEvent(new MessageInterceptorClass, "onMessageReceive", \Nebucord\Base\StatusList::GWEVT_MESSAGE_CREATE);

$nebucord = new Nebucord(['token' => 'your_bot_token', 'ctrlusr' => ['controluser-snowflake1', 'controluser-snowflake2']]);
$nebucord->bootstrap()
    ->setEventTable($nebucordEventTable)
    ->run();

The above example prints out every message which was seen by the bot (based on the bot roles on the Discord guild). "Seen" means all CREATE_MESSAGE events from the Discord gateway.

Now on a console:

user@linux:~# php -f <your_php_file>.php

Example usage - REST API

Basic usage for sending a message:

<?php
include "vendor/autoload.php"; // Composer autloader

use Nebucord\NebucordREST;

$nebucordREST = new NebucordREST(['token' => 'your_bot_token']);
$message_model = $nebucordREST->createRESTExecutor()->createRESTActionFromArray(
   \Nebucord\REST\Base\RestStatusList::REST_CREATE_MESSAGE,
   [
       'channel_id' => 123123123123,
       'content' => "message"
   ]
);

"$message_model" is an object with the answer from the REST gateway.

Basic usage for receiving guild channels:

<?php
include "vendor/autoload.php";

use Nebucord\NebucordREST;

$nebucordREST = new NebucordREST(['token' => 'your_bot_token']);
$channels = $nebucordREST->createRESTExecutor()->createRESTActionFromArray(
   \Nebucord\REST\Base\RestStatusList::REST_GET_GUILD_CHANNELS,
   [
       'guild_id' => 123123123123123,
   ]
);

"$channels" is an array of channel models for processing.

createRESTExecutor() returns an object which can be re-used for creating REST request.
It is also possible to create a REST request with createRESTAction(string Nebucord_RESTStatus::REST_*, ModelREST $restmodel) instead of createRESTActionFromArray(string Nebucord_RESTStatus::REST_*, array $restparams) and pass a predefined ModelREST object with all params for the REST gateway.

More info

For more information see: Discord Developer Portal

nebucord's People

Contributors

eurobertics avatar kschu91 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

nebucord's Issues

Don't hardcode LOGFILE_PATH

Not all servers run on apache, and /var/log/apache2 is not always an appropriate place for log files. If the directory doesn't exist, the program crashes Nebucord_Logger should take a log directory as a parameter.

Not Running Correctly

I'm trying to run this but it's giving me issues.

root@aeronus-webserver:/var/www/html/bot# php -f bot.php 29-12-2019 01:24:18 - nebucordws.nebulatien.org - info | Starting Nebucord v. 0.8.2 on nebucordws.nebulatien.org 29-12-2019 01:24:18 - nebucordws.nebulatien.org - info | Starting HTTP client... 29-12-2019 01:24:18 - nebucordws.nebulatien.org - info | Eventtable set... 29-12-2019 01:24:18 - nebucordws.nebulatien.org - info | No actiontable set, using default one... 29-12-2019 01:24:18 - nebucordws.nebulatien.org - info | Connecting to Discord WebSocket gateway... 29-12-2019 01:24:18 - nebucordws.nebulatien.org - info | WebSocket connection established. 29-12-2019 01:24:18 - nebucordws.nebulatien.org - info | Nebucord set up, entering main loop... 29-12-2019 01:24:18 - nebucordws.nebulatien.org - info | Event received: READY - Sequence: 1 PHP Notice: Undefined index: ctrlusr in /var/www/html/bot/vendor/eurobertics/nebucord/src/Controller/Nebucord_RuntimeController.php on line 272 29-12-2019 01:24:18 - nebucordws.nebulatien.org - info | Event received: - Sequence: 29-12-2019 01:24:18 - nebucordws.nebulatien.org - warn | Creating default model, no appropriate model for event found. PHP Notice: Undefined offset: 0 in /var/www/html/bot/vendor/eurobertics/nebucord/src/Controller/Nebucord_EventController.php on line 135 PHP Notice: Undefined offset: 0 in /var/www/html/bot/vendor/eurobertics/nebucord/src/Controller/Nebucord_EventController.php on line 135 PHP Warning: call_user_func() expects parameter 1 to be a valid callback, first array member is not a valid class name or object in /var/www/html/ bot/vendor/eurobertics/nebucord/src/Controller/Nebucord_EventController.php on line 135 29-12-2019 01:24:18 - nebucordws.nebulatien.org - info | Event received: - Sequence:

Nebucord threading

Hello,

I'm the maintainer of a PHP game chatbot named Bebot (forum @ bebot.shadow-realm.org) and i'm considering Nebucord as an API source candidate for a Discord module i'm planning soon.

But the problem is once i included Nebucord as a module and initiated its connection (bootstrap->run), my chatbot is 100% busy on the Nebucord websocket and doesn't respond anything else.

Would you see a flexible way to include Nebucord as child process while keeping the main thread available for other chatbot tasks ? I've been thinking of using PHP 7 pthreads but it would require full reinstall ...

Any suggestion is welcomed !

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.