Table of contents
- Mod log
- Basic Usage
- Registering Case types
- API Reference
- Mod log
- class redbot.core.modlog.Case
- bot
- guild
- created_at
- action_type
- user
- NOTE
- moderator
- NOTE
- case_number
- reason
- until
- channel
- NOTE
- parent_channel_id
- amended_by
- NOTE
- modified_at
- message
- last_known_username
- extra_info
- await edit(data)
- classmethod await from_json(mod_channel, bot, case_number, data, **kwargs)
- await message_content(embed=True)
- property parent_channel
- to_json()
- class redbot.core.modlog.CaseType(name, default_setting, image, case_str, guild=None, **kwargs)
- name
- default_setting
- image
- case_str
- classmethod from_json(name, data, **kwargs)
- await is_enabled()
- await set_enabled(enabled)
- await to_json()
- await redbot.core.modlog.create_case(bot, guild, created_at, action_type, user, moderator=None, reason=None, until=None, channel=None, last_known_username=None, extra_info=None)
- await redbot.core.modlog.delete_case(case_number, guild, bot, , delete_message=True)
- await redbot.core.modlog.get_all_cases(guild, bot)
- await redbot.core.modlog.get_all_casetypes(guild=None)
- await redbot.core.modlog.get_case(case_number, guild, bot)
- await redbot.core.modlog.get_cases_for_member(guild, bot, , member=None, member_id=None)
- await redbot.core.modlog.get_casetype(name, guild=None)
- await redbot.core.modlog.get_latest_case(guild, bot)
- await redbot.core.modlog.get_modlog_channel(guild)
- await redbot.core.modlog.register_casetype(name, default_setting, image, case_str)
- await redbot.core.modlog.register_casetypes(new_types)
- await redbot.core.modlog.reset_cases(guild)
- await redbot.core.modlog.set_modlog_channel(guild, channel)
This page is generated from
docs/framework_modlog.rst- please edit the source file instead of this page.
Mod log
Basic Usage
from redbot.core import commands, modlog
import discord
class MyCog(commands.Cog):
@commands.command()
@commands.admin_or_permissions(ban_members=True)
async def ban(self, ctx, user: discord.Member, reason: str = None):
await ctx.guild.ban(user)
case = await modlog.create_case(
ctx.bot, ctx.guild, ctx.message.created_at, action_type="ban",
user=user, moderator=ctx.author, reason=reason
)
await ctx.send("Done. It was about time.")
Registering Case types
To register case types, use a special cog_load() method which is called when you add a cog:
# mycog/mycog.py
from redbot.core import modlog, commands
import discord
class MyCog(commands.Cog):
async def cog_load(self):
await self.register_casetypes()
@staticmethod
async def register_casetypes():
# Registering a single casetype
ban_case = {
"name": "ban",
"default_setting": True,
"image": "\N{HAMMER}",
"case_str": "Ban",
}
try:
await modlog.register_casetype(**ban_case)
except RuntimeError:
pass
# Registering multiple casetypes
new_types = [
{
"name": "hackban",
"default_setting": True,
"image": "\N{BUST IN SILHOUETTE}\N{HAMMER}",
"case_str": "Hackban",
},
{
"name": "kick",
"default_setting": True,
"image": "\N{WOMANS BOOTS}",
"case_str": "Kick",
}
]
await modlog.register_casetypes(new_types)
# mycog/__init__.py
from .mycog import MyCog
async def setup(bot):
cog = MyCog()
await bot.add_cog(cog)
IMPORTANT
Image should be the emoji you want to represent your case type with.
API Reference
Mod log
class redbot.core.modlog.Case
Bases: object
A single mod log case
This class should ONLY be instantiated by the modlog itself.
bot
The bot object.
- Type: Red
guild
The guild the action was taken in.
- Type: discord.Guild
created_at
The UNIX time the action occurred at.
- Type: int
action_type
The type of action that was taken.
- Type: str
user
The user target by the action.
NOTE
This attribute will be of type int
if the Discord user can no longer be found.
- Type: Union[discord.abc.User, int]
moderator
The moderator who took the action.
None if the moderator is unknown.
NOTE
This attribute will be of type int
if the Discord user can no longer be found.
- Type: Optional[Union[discord.abc.User, int]]
case_number
The case's number.
- Type: int
reason
The reason the action was taken.
None if the reason was not specified.
- Type: Optional[str]
until
The UNIX time the action is in effect until.
None if the action is permanent.
- Type: Optional[int]
channel
The channel the action was taken in.
None if the action was not related to a channel.
NOTE
This attribute will be of type int
if the channel seems to no longer exist.
- Type: Optional[Union[discord.abc.GuildChannel, discord.Thread, int]]
parent_channel_id
The parent channel ID of the thread in channel.
None if the action was not done in a thread.
- Type: Optional[int]
amended_by
The moderator who made the last change to the case.
None if the case was never edited.
NOTE
This attribute will be of type int
if the Discord user can no longer be found.
- Type: Optional[Union[discord.abc.User, int]]
modified_at
The UNIX time of the last change to the case.
None if the case was never edited.
- Type: Optional[float]
message
The message created by Modlog for this case.
Instance of discord.Message if the Case object was returned from
modlog.create_case(), otherwise discord.PartialMessage.
None if we know that the message no longer exists
(note: it might not exist regardless of whether this attribute is None)
or if it has never been created.
- Type: Optional[Union[discord.PartialMessage, discord.Message]]
last_known_username
The last known user handle (username / username#1234) of the user.
None if the handle of the user was never saved
or if their data had to be anonymized.
- Type: Optional[str]
extra_info
Extra information shown in the case, separately from the reason (it is kept when the reason is changed).
- Type: Optional[str]
await edit(data)
Edits a case
- Parameters: data (dict) -- The attributes to change
classmethod await from_json(mod_channel, bot, case_number, data, **kwargs)
Get a Case object from the provided information
- Parameters:
- mod_channel (
discord.TextChannelordiscord.VoiceChannel,discord.StageChannel) -- The mod log channel for the guild - bot (Red) -- The bot's instance. Needed to get the target user
- case_number (int) -- The case's number.
- data (dict) -- The JSON representation of the case to be gotten
- **kwargs -- Extra attributes for the Case instance which override values in the data dict. These should be complete objects and not IDs, where possible.
- mod_channel (
- Returns: The case object for the requested case
- Return type: Case
- Raises:
- discord.NotFound -- The user the case is for no longer exists
- discord.Forbidden -- Cannot read message history to fetch the original message.
- discord.HTTPException -- A generic API issue
await message_content(embed=True)
Format a case message
- Parameters: embed (bool) -- Whether or not to get an embed
- Returns: A rich embed or string representing a case message
- Return type:
discord.Embed or
str
property parent_channel
The parent text/forum channel of the thread in channel.
This will be None if channel is not a thread
and when the parent text/forum channel is not in cache (probably due to removal).
to_json()
Transform the object to a dict
- Returns: The case in the form of a dict
- Return type: dict
class redbot.core.modlog.CaseType(name, default_setting, image, case_str, guild=None, **kwargs)
Bases: object
A single case type
This class should ONLY be instantiated by the modlog itself.
name
The name of the case
- Type: str
default_setting
Whether the case type should be on (if True)
or off (if False) by default
- Type: bool
image
The emoji to use for the case type (for example, 👢)
- Type: str
case_str
The string representation of the case (example: Ban)
- Type: str
classmethod from_json(name, data, **kwargs)
- Parameters:
- Returns: The case type object created from given data.
- Return type: CaseType
await is_enabled()
Determines if the case is enabled. If the guild is not set, this will always return False
-
Returns: True if the guild is set and the casetype is enabled for the guild
False if the guild is not set or if the guild is set and the type is disabled
-
Return type: bool
await set_enabled(enabled)
Sets the case as enabled or disabled
- Parameters: enabled (bool) -- True if the case should be enabled, otherwise False
await to_json()
Transforms the case type into a dict and saves it
await redbot.core.modlog.create_case(bot, guild, created_at, action_type, user, moderator=None, reason=None, until=None, channel=None, last_known_username=None, extra_info=None)
Creates a new case.
This fires an event on_modlog_case_create
- Parameters:
- bot (Red) -- The bot object
- guild (discord.Guild) -- The guild the action was taken in
- created_at (datetime) -- The time the action occurred at.
If naive
datetimeobject is passed, it's treated as a local time (similarly to how Python treats naivedatetimeobjects). - action_type (str) -- The type of action that was taken
- user (Union [discord.Object , discord.abc.User , int ]) -- The user target by the action
- moderator (Optional *[*Union [discord.Object , discord.abc.User , int ] ]) -- The moderator who took the action
- reason (Optional [str ]) -- The reason the action was taken
- until (Optional *[*datetime ]) -- The time the action is in effect until.
If naive
datetimeobject is passed, it's treated as a local time (similarly to how Python treats naivedatetimeobjects). - channel (Optional *[*Union [discord.abc.GuildChannel , discord.Thread ] ]) -- The channel the action was taken in
- last_known_username (Optional [str ]) -- The last known user handle (
username/username#1234) of the user Note: This is ignored if a Member or User object is provided in the user field - extra_info (Optional [str ]) -- Extra information to show in the case, separately from the reason.
Unlike the reason, this is not changed by
[p]reason.
- Raises:
- ValueError -- If the action type is not a valid action type.
- RuntimeError -- If user is the bot itself.
- TypeError -- If
channelis of typediscord.PartialMessageable.
await redbot.core.modlog.delete_case(case_number, guild, bot, , delete_message=True)
Deletes the modlog case with the specified number.
The numbers of other cases are not changed.
Versionadded
Added in version 3.6.0.
- Parameters:
- case_number (int) -- The number of the case to delete.
- guild (discord.Guild) -- The guild to delete the case from.
- bot (Red) -- The bot's instance.
- delete_message (bool) -- Whether to also delete the case's message in the modlog channel (if possible).
- Raises: RuntimeError -- If there is no case for the specified number.
await redbot.core.modlog.get_all_cases(guild, bot)
Gets all cases for the specified guild
- Parameters:
- guild (
discord.Guild) -- The guild to get the cases from - bot (Red) -- The bot's instance
- guild (
- Returns: A list of all cases for the guild
- Return type: list
await redbot.core.modlog.get_all_casetypes(guild=None)
Get all currently registered case types
- Returns: A list of case types
- Return type: list
await redbot.core.modlog.get_case(case_number, guild, bot)
Gets the case with the associated case number
- Parameters:
- case_number (int) -- The case number for the case to get
- guild (discord.Guild) -- The guild to get the case from
- bot (Red) -- The bot's instance
- Returns: The case associated with the case number
- Return type: Case
- Raises: RuntimeError -- If there is no case for the specified number
await redbot.core.modlog.get_cases_for_member(guild, bot, , member=None, member_id=None)
Gets all cases for the specified member or member id in a guild.
- Parameters:
- guild (
discord.Guild) -- The guild to get the cases from - bot (Red) -- The bot's instance
- member (
discord.Member) -- The member to get cases about - member_id (int) -- The id of the member to get cases about
- guild (
- Returns: A list of all matching cases.
- Return type: list
- Raises:
- ValueError -- If at least one of member or member_id is not provided
- discord.Forbidden -- The bot does not have permission to fetch the modlog message which was sent.
- discord.HTTPException -- Fetching the user failed.
await redbot.core.modlog.get_casetype(name, guild=None)
Gets the case type
- Parameters:
- name (str) -- The name of the case type to get
- guild (Optional [discord.Guild ]) -- If provided, sets the case type's guild attribute to this guild
- Returns:
Case type with provided name. If such case type doesn't exist this will be
None. - Return type: Optional[CaseType]
await redbot.core.modlog.get_latest_case(guild, bot)
Get the latest case for the specified guild.
- Parameters:
- guild (discord.Guild) -- The guild to get the latest case for.
- bot (Red) -- The bot object.
- Returns:
The latest case object.
Noneif it the guild has no cases. - Return type: Optional[Case]
await redbot.core.modlog.get_modlog_channel(guild)
Get the current modlog channel.
- Parameters:
guild (
discord.Guild) -- The guild to get the modlog channel for. - Returns: The channel object representing the modlog channel.
- Return type:
discord.TextChannel,discord.VoiceChannel, ordiscord.StageChannel - Raises: RuntimeError -- If the modlog channel is not found.
await redbot.core.modlog.register_casetype(name, default_setting, image, case_str)
Registers a case type. If the case type exists and there are differences between the values passed and what is stored already, the case type will be updated with the new values
- Parameters:
- Returns: The case type that was registered
- Return type: CaseType
- Raises:
- RuntimeError -- If the case type is already registered
- TypeError -- If a parameter is missing
- ValueError -- If a parameter's value is not valid
await redbot.core.modlog.register_casetypes(new_types)
Registers multiple case types
- Parameters: new_types (list) -- The new types to register
- Returns:
Trueif all were registered successfully - Return type: bool
- Raises:
- KeyError --
- ValueError --
- AttributeError --
SEE ALSO
redbot.core.modlog.register_casetype
await redbot.core.modlog.reset_cases(guild)
Wipes all modlog cases for the specified guild.
- Parameters:
guild (
discord.Guild) -- The guild to reset cases for
await redbot.core.modlog.set_modlog_channel(guild, channel)
Changes the modlog channel
- Parameters:
- guild (
discord.Guild) -- The guild to set a mod log channel for - channel (
discord.TextChannel,discord.VoiceChannel,discord.StageChannel, orNone) -- The channel to be set as modlog channel
- guild (
- Returns:
Trueif successful - Return type: bool
Installation Guides
- Installing Red
- Creating a bot account
- Updating Red
- Backing Up and Restoring Red
- About Virtual Environments
- Setting up auto-restart using batch on Windows
- Setting up auto-restart on Mac
- Setting up auto-restart using systemd on Linux
Cog Reference
User guides
- Getting started
- Running Red on Fluxer
- About Privileged Intents
- About public (big) bots
- Admin
- Alias
- Audio
- Cleanup
- Cog Manager UI
- Core
- CustomCommands
- Dev
- Downloader
- Economy
- Filter
- General
- Image
- Permissions
- Mod
- ModLog
- Mutes
- Reports
- Streams
- Trivia
- Warnings
- Red and End User Data
Cog Creation Guides
- Creating cogs for Red
- Slash Commands and Interactions
- Publishing cogs for Red
- Becoming an Approved Cog Creator
- Versioning
- End-user Guarantees
- Developer Guarantees
- Breaking Change Notices
Cog Development Framework Reference
- Shared API Keys
- Bank
- Bot
- Command Check Decorators
- App Command Check Decorators
- Commands Package
- App Commands Package
- Config
- Data Manager
- Custom Events
- Internationalization Framework
- Mod log
- RPC
- Tree
- Utility Functions
Others
Legacy (Red V2)