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)[源代码]

基类: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)[源代码]

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

evennia.server.portal.discord.should_retry(status_code)[源代码]

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

参数

status_code (int) –

返回

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

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

基类: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)[源代码]

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)[源代码]
websocket_init(payload, *args, **kwargs)[源代码]

callback for when the URL is gotten

buildProtocol(addr)[源代码]

Build new instance of protocol

参数

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

startedConnecting(connector)[源代码]

Tracks reconnections for debugging.

参数

connector (Connector) – Represents the connection.

reconnect()[源代码]

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

start()[源代码]

Connect protocol to remote server

class evennia.server.portal.discord.DiscordClient[源代码]

基类: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__()[源代码]

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

at_login()[源代码]
onOpen()[源代码]

Called when connection is established.

onMessage(payload, isBinary)[源代码]

Callback fired when a complete WebSocket message was received.

参数
  • 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)[源代码]

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

参数
  • 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)[源代码]

Process the response from sending a POST request

参数

body (bytes) –

handle_error(data, **kwargs)[源代码]

General hook for processing errors.

参数

data (dict) –

resume()[源代码]

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

disconnect(reason=None)[源代码]

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

参数

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

identify(*args, **kwargs)[源代码]

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

connection_ready(data)[源代码]

Process READY data for relevant bot info.

doHeartbeat(*args, **kwargs)[源代码]

Send heartbeat to Discord.

send_channel(text, channel_id, **kwargs)[源代码]

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

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

send_nickname(text, guild_id, user_id, **kwargs)[源代码]

Changes a user’s nickname on a Discord server.

Use with session.msg(nickname=(new_nickname, guild_id, user_id))

send_role(role_id, guild_id, user_id, **kwargs)[源代码]
send_default(*args, **kwargs)[源代码]

Ignore other outputfuncs

data_in(data, **kwargs)[源代码]

Process incoming data from Discord and sent to the Evennia server

参数

data (dict) – Converted json data.