Abc Reference

This Protocol simply defines how a Cache should work. This is going to only be useful if you either plan on working directly with an existing cache or wish to build your own.

Any form of internal cache is guranteed to implement this so you can treat it as a source of truth for usage. (Unless you bypass them)

class antispam.abc.Cache(*args, **kwargs)

A generic Protocol for any Cache to implement

add_message(message: antispam.dataclasses.message.Message) → None

Adds a Message to the relevant Member, creating the Guild/Member if they don’t exist

Parameters:message (Message) – The Message to add to the internal cache

Notes

This should silently create any Guild’s/Member’s required to fulfil this transaction

delete_guild(guild_id: int) → None

Removes a guild from the cache.

Parameters:guild_id (int) – The id of the guild we wish to remove

Notes

This fails silently.

delete_member(member_id: int, guild_id: int) → None

Removes a member from the cache.

Parameters:
  • member_id (int) – The id of the member we wish to remove
  • guild_id (int) – The guild this member is in

Notes

This fails silently.

drop() → None

Drops the entire cache, deleting everything contained within.

get_all_guilds() → AsyncIterable[antispam.dataclasses.guild.Guild]

Returns a generator containing all cached guilds

Yields:Guild – A generator of all stored guilds
get_all_members(guild_id: int) → AsyncIterable[antispam.dataclasses.member.Member]

Fetches all members within a guild and returns them within a generator

Parameters:guild_id (int) – The guild we want members in
Yields:Member – All members in the given guild
Raises:GuildNotFound – The given guild was not found
get_guild(guild_id: int) → antispam.dataclasses.guild.Guild

Fetch a Guild dataclass populated with members

Parameters:guild_id (int) – The id of the Guild to retrieve from cache
Raises:GuildNotFound – A Guild could not be found in the cache with the given id
get_member(member_id: int, guild_id: int) → antispam.dataclasses.member.Member

Fetch a Member dataclass populated with messages

Parameters:
  • member_id (int) – The id of the member to fetch from cache
  • guild_id (int) – The id of the guild this member is associated with
Raises:
  • MemberNotFound – This Member could not be found on the associated Guild within the internal cache
  • GuildNotFound – The relevant guild could not be found
initialize(*args, **kwargs) → None

This method gets called once when the AntiSpamHandler init() method gets called to allow for setting up connections, etc

Notes

This is not required.

reset_member_count(member_id: int, guild_id: int, reset_type: antispam.enums.reset_type.ResetType) → None

Reset the chosen enum type back to the default value

Parameters:
  • member_id (int) – The Member to reset
  • guild_id (int) – The guild this member is in
  • reset_type (ResetType) – An enum denoting the type of reset
set_guild(guild: antispam.dataclasses.guild.Guild) → None

Stores a Guild in the cache

This is essentially a UPSERT operation

Parameters:guild (Guild) – The Guild that needs to be stored
set_member(member: antispam.dataclasses.member.Member) → None

Stores a Member internally and attaches them to a Guild, creating the Guild silently if required

Essentially an UPSERT operation

Parameters:member (Member) – The Member we want to cache
class antispam.abc.Lib(*args, **kwargs)

A protocol to extend and implement for any libs that wish to hook into this package and work natively.

Notes

Not public api. For internal usage only.

check_message_can_be_propagated(message) → antispam.dataclasses.propagate_data.PropagateData

Given a message from the relevant package, run all checks to check if this message should be propagated.

Parameters:message (Union[discord.Message, hikari.messages.Message]) – The message to check
Returns:The data required within propagate
Return type:PropagateData
Raises:PropagateFailure – This raises an error with the .data attribute set. .data is what get returned from within propagate
create_message(message) → antispam.dataclasses.message.Message

Given a message to extract data from, create and return a Message class

Parameters:message (Union[discord.Message, hikari.messages.Message]) – The message to extract data from
Returns:The flushed out message
Return type:Message
delete_member_messages(member: antispam.dataclasses.member.Member) → None

Given a member, traverse all duplicate messages and delete them.

Parameters:member (Member) – The member whose messages should be deleted

Notes

Just call delete_message on each message

delete_message(message) → None

Given a message, call and handle the relevant deletion contexts.

Parameters:message (Union[discord.Message, hikari.messages.Message]) – The message to delete

Notes

This should handle given errors silently.

dict_to_embed(data: dict, message, warn_count: int, kick_count: int)
Parameters:
  • data (dict) – The data to build an embed from
  • message (Union[discord.Message, hikari.messages.Message]) – The message to extract data from
  • warn_count (int) – How many warns this person has
  • kick_count (int) – How many kicks this person has
Returns:

Return type:

Union[discord.Embed, hikari.embeds.Embed]

embed_to_string(embed) → str

Given an embed, return a string representation

Parameters:embed (Union[discord.Embed, hikari.embeds.Embed]) – The embed to cast to string
Returns:The embed as a string
Return type:str
get_channel_by_id(channel_id: int)

Returns the given channel for the id

get_channel_from_message(message)

Returns the channel for a message

get_channel_id(message) → int

Returns the channel id of this message

get_file(path: str)

Returns a discord file object for the given path

get_guild_id(message) → int

Returns the guild id of this message

get_message_mentions(message)

Returns all the mentions from a message

punish_member(original_message, member: antispam.dataclasses.member.Member, internal_guild: antispam.dataclasses.guild.Guild, user_message, guild_message, is_kick: bool, user_delete_after: int = None, channel_delete_after: int = None)

A generic method to handle multiple methods of punishment for a user. Supports: kicking, banning

Parameters:
  • member (Member) – A reference to the member we wish to punish
  • internal_guild (Guild) – A reference to the guild this member is in
  • original_message (Union[discord.Message, hikari.messages.Message]) – Where we get everything from :)
  • user_message (Union[str, discord.Embed, hikari.embeds.Embed]) – A message to send to the user who is being punished
  • guild_message (Union[str, discord.Embed, hikari.embeds.Embed]) – A message to send in the guild for whoever is being punished
  • is_kick (bool) – Is it a kick? Else ban
  • user_delete_after (int, Optional) – An int value denoting the time to delete user sent messages after
  • channel_delete_after (int, Optional) – An int value denoting the time to delete channel sent messages after
Raises:

MissingGuildPermissions – I lack perms to carry out this punishment

send_guild_log(guild, message, delete_after_time: Optional[int], original_channel, file=None) → None

Sends a message to the guilds log channel

Notes

If no log channel, send in ctx.channel

Parameters:

Notes

This should catch any sending errors, log them and then proceed to return None

send_message_to_(target, message, mention: str, delete_after_time: Optional[int] = None) → None

Given a message and target, send :param target: Where to send the message :type target: Union[discord.abc.Messageable, hikari TODO doc this] :param message: The message to send :type message: Union[str, discord.Embed, hikari.embeds.Embed] :param mention: A string denoting a raw mention of the punished user :type mention: str :param delete_after_time: When to delete the message after :type delete_after_time: Optional[int]

Notes

This should implement Options.mention_on_embed

substitute_args(message: str, original_message, warn_count: int, kick_count: int) → str

Given a message, substitute in relevant arguments and return a valid string

Parameters:
  • message (str) – The message to substitute args into
  • original_message (Union[discord.Message, hikari.messages.Message]) – The message to extract data from
  • warn_count (int) – How many warns this person has
  • kick_count (int) – How many kicks this person has
Returns:

The message with substituted args

Return type:

str

transform_message(item: Union[str, dict], message, warn_count: int, kick_count: int)
Parameters:
Returns:

  • Union[str, discord.Embed, hikari.embeds.Embed]
  • A template complete message ready for sending

visualizer(content: str, message, warn_count: int = 1, kick_count: int = 2)

Returns a message transformed as if the handler did it

Parameters:
Returns:

The transformed content

Return type:

Union[str, discord.Embed]