evennia.server.portal.discord

Implements Discord chat channel integration.

The Discord API uses a mix of websockets and REST API endpoints.

In order for this integration to work, you need to have your own discord bot set up via https://discord.com/developers/applications with the MESSAGE CONTENT toggle switched on, and your bot token added to server/conf/secret_settings.py as your DISCORD_BOT_TOKEN

evennia.server.portal.discord.random() → x in the interval [0, 1).
class evennia.server.portal.discord.QuietConnectionPool(reactor, persistent=True)[source]

Bases: twisted.web.client.HTTPConnectionPool

A quiet version of the HTTPConnectionPool which sets the factory’s noisy property to False to muffle log output.

__init__(reactor, persistent=True)[source]

Initialize self. See help(type(self)) for accurate signature.

evennia.server.portal.discord.should_retry(status_code)[source]

Helper function to check if the request should be retried later.

Parameters

status_code (int) –

Returns

retry (bool) - True if request should be retried False otherwise

class evennia.server.portal.discord.DiscordWebsocketServerFactory(sessionhandler, *args, **kwargs)[source]

Bases: autobahn.twisted.websocket.WebSocketClientFactory, twisted.internet.protocol.ReconnectingClientFactory

A customized websocket client factory that navigates the Discord gateway process.

initialDelay = 1
factor = 1.5
maxDelay = 60
noisy = False
gateway = None
resume_url = None
is_connecting = False
__init__(sessionhandler, *args, **kwargs)[source]

In addition to all arguments to the constructor of :func:autobahn.websocket.interfaces.IWebSocketClientChannelFactory, you can supply a **reactor** keyword argument to specify the Twisted reactor to be used.

get_gateway_url(*args, **kwargs)[source]
websocket_init(payload, *args, **kwargs)[source]

callback for when the URL is gotten

buildProtocol(addr)[source]

Build new instance of protocol

Parameters

addr (str) – Not used, using factory/settings data

startedConnecting(connector)[source]

Tracks reconnections for debugging.

Parameters

connector (Connector) – Represents the connection.

reconnect()[source]

Force a reconnection of the bot protocol. This requires de-registering the session and then reattaching a new one.

start()[source]

Connect protocol to remote server

class evennia.server.portal.discord.DiscordClient[source]

Bases: autobahn.twisted.websocket.WebSocketClientProtocol, evennia.server.session.Session

Implements the Discord client

nextHeartbeatCall = None
pending_heartbeat = False
heartbeat_interval = None
last_sequence = 0
session_id = None
discord_id = None
__init__()[source]

Initialize self. See help(type(self)) for accurate signature.

at_login()[source]
onOpen()[source]

Called when connection is established.

onMessage(payload, isBinary)[source]

Callback fired when a complete WebSocket message was received.

Parameters
  • payload (bytes) – The WebSocket message received.

  • isBinary (bool) – Flag indicating whether payload is binary or UTF-8 encoded text.

onClose(wasClean, code=None, reason=None)[source]

This is executed when the connection is lost for whatever reason. it can also be called directly, from the disconnect method.

Parameters
  • wasClean (bool) – **True** if the WebSocket was closed cleanly.

  • code (int or None) – Close status as sent by the WebSocket peer.

  • reason (str or None) – Close reason as sent by the WebSocket peer.

post_response(body, **kwargs)[source]

Process the response from sending a POST request

Parameters

body (bytes) –

handle_error(data, **kwargs)[source]

General hook for processing errors.

Parameters

data (dict) –

resume()[source]

Called after a reconnection to re-identify and replay missed events

disconnect(reason=None)[source]

Generic hook for the engine to call in order to disconnect this protocol.

Parameters

reason (str or None) – Motivation for the disconnection.

identify(*args, **kwargs)[source]

Send Discord authentication. This should be sent once heartbeats begin.

connection_ready(data)[source]

Process READY data for relevant bot info.

doHeartbeat(*args, **kwargs)[source]

Send heartbeat to Discord.

send_channel(text, channel_id, **kwargs)[source]

Send a message from an Evennia channel to a Discord channel.

Use with session.msg(channel=(message, channel, sender))

send_default(*args, **kwargs)[source]

Ignore other outputfuncs

data_in(data, **kwargs)[source]

Process incoming data from Discord and sent to the Evennia server

Parameters

data (dict) – Converted json data.