Discord

Main module

Discord related functionalities

sni.discord.discord.complete_authentication_challenge(discord_user: discord.user.User, code: str)[source]

Complete an authentication challenge, see sni.discord.discord.new_authentication_challenge().

async sni.discord.discord.ensure_role_for_group(grp: sni.user.models.Group)[source]

Ensure that a Discord role exists for the given group, and returns it.

sni.discord.discord.new_authentication_challenge(usr: sni.user.models.User) → str[source]

Creates a new authentication challenge.

The challenge proceeds as follows:

  1. A user (sni.user) asks to start a challenge by calling this method.

  2. This methods returns a random code, and the user has 60 seconds type !auth <code> in the dedicated authentication channel.

  3. The Discord client is notified, and check this code against the pending Discort authentication challenges.

Discord bot

Discord Bot management. The bot requires the bot scope, and the following permissions: * Manage Roles * Change Nickname * Manage Nicknames * Send Messages * Manage Messages which corresponds to the the permission integer 469772288. Therefore, the invitation link for the bot should look like this: https://discord.com/api/oauth2/authorize?client_id=<bot_id>&permissions=469772288&scope=bot. .. seealso:: Discord developer portal, Creating a Bot Account

sni.discord.bot.JOBS_KEY: str = 'scheduler:discord:jobs'

The redis key for the job list

sni.discord.bot.RUN_TIMES_KEY: str = 'scheduler:discord:run_times'

The redis key for the job run times

sni.discord.bot.get_guild() → discord.guild.Guild[source]

Returns a guild handler corresponding to the discord.server_id setting.

sni.discord.bot.get_member(user_id: int) → discord.member.Member[source]

Gets a guild member by its user ID.

async sni.discord.bot.log(message: str)[source]

Sends a message on the logging channel. If configuration key discord.log_channel_id is None, does’t do anything.

sni.discord.bot.start_bot()[source]

Runs the discord client in a different thread.

sni.discord.bot.start_scheduler() → None[source]

Clears the job store and starts the scheduler.

sni.discord.bot.stop_scheduler() → None[source]

Stops the scheduler and cleans up things

Discord bot commands

Discord bot commands

Discord bot events

Discord events

async sni.discord.events.on_disconnect()[source]

Called when the Discord client has been disconnected. Executes cleanup tasks.

async sni.discord.events.on_ready()[source]

Called when the discord client is ready. Starts the discord client scheduler.

Database models

Database models

class sni.discord.models.DiscordAuthenticationChallenge(*args, **values)[source]

Bases: mongoengine.document.Document

Represents a pending authentication challenge.

code

Challenge code

created_on

Timestamp of the creation of this document

user

User that stated this authentication challenge

Jobs

Discord jobs.

These jobs run on a dedicated asyncio scheduler.

async sni.discord.jobs.update_discord_role(grp: sni.user.models.Group)[source]

Updates a discord role. Makes sure all members of the group have that role, and demotes members that should not have it.

async sni.discord.jobs.update_discord_roles()[source]

Updates discord role. A SNI group is mapped to a discord role unless that group’s map_to_discord if False

async sni.discord.jobs.update_discord_user(usr: sni.user.models.User)[source]

Updates a discord user. Doesn’t modify their roles.

async sni.discord.jobs.update_discord_users()[source]

Updates all discord users.

Database signals

Database signals. See Mongoengine signals

Migration

Database migrations

sni.discord.migration.migrate() → None[source]

Runs migration tasks