Example usages¶
Note, all of these examples are for discord.py. If you would like another library here, let me know.
Super duper basic bot¶
1import discord
2from discord.ext import commands
3
4from antispam import AntiSpamHandler
5
6bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())
7bot.handler = AntiSpamHandler(bot)
8
9
10@bot.event
11async def on_ready():
12 # On ready, print some details to standard out
13 print(f"-----\nLogged in as: {bot.user.name} : {bot.user.id}\n-----")
14
15
16@bot.event
17async def on_message(message):
18 await bot.handler.propagate(message)
19 await bot.process_commands(message)
20
21
22if __name__ == "__main__":
23 bot.run("Bot Token Here")
Basic Hikari bot¶
1import hikari
2from antispam import AntiSpamHandler
3from antispam.enums import Library
4
5bot = hikari.GatewayBot(
6 token="..."
7)
8handler = AntiSpamHandler(bot, library=Library.HIKARI)
9
10@bot.listen()
11async def ping(event: hikari.GuildMessageCreateEvent) -> None:
12 if event.is_bot or not event.content:
13 return
14
15 await handler.propagate(event.message)
16
17bot.run()
How to use templating in a string¶
1from discord.ext import commands
2
3from antispam import AntiSpamHandler, Options
4
5bot = commands.Bot(command_prefix="!")
6bot.handler = AntiSpamHandler(bot, options=Options(ban_message="$MENTIONUSER you are hereby banned from $GUILDNAME for spam!"))
7
8@bot.event
9async def on_ready():
10 print(f"-----\nLogged in as: {bot.user.name} : {bot.user.id}\n-----")
11
12@bot.event
13async def on_message(message):
14 await bot.handler.propagate(message)
15 await bot.process_commands(message)
16
17if __name__ == "__main__":
18 bot.run("Bot Token")
Cog Based Usage¶
1from discord.ext import commands
2from antispam import AntiSpamHandler
3
4class AntiSpamCog(commands.Cog):
5 def __init__(self, bot):
6 self.bot = bot
7 self.bot.handler = AntiSpamHandler(self.bot)
8
9 @commands.Cog.listener()
10 async def on_ready(self):
11 print("AntiSpamCog is ready!\n-----\n")
12
13 @commands.Cog.listener()
14 async def on_message(self, message):
15 await self.bot.handler.propagate(message)
16
17def setup(bot):
18 bot.add_cog(AntiSpamCog(bot))
How to use templating in embeds¶
1from discord.ext import commands
2
3from antispam import AntiSpamHandler, Options
4
5bot = commands.Bot(command_prefix="!")
6
7warn_embed_dict = {
8 "title": "**Dear $USERNAME**",
9 "description": "You are being warned for spam, please stop!",
10 "timestamp": True,
11 "color": 0xFF0000,
12 "footer": {"text": "$BOTNAME", "icon_url": "$BOTAVATAR"},
13 "author": {"name": "$GUILDNAME", "icon_url": "$GUILDICON"},
14 "fields": [
15 {"name": "Current warns:", "value": "$WARNCOUNT", "inline": False},
16 {"name": "Current kicks:", "value": "$KICKCOUNT", "inline": False},
17 ],
18}
19bot.handler = AntiSpamHandler(bot, options=Options(guild_warn_message=warn_embed_dict))
20
21@bot.event
22async def on_ready():
23 print(f"-----\nLogged in as: {bot.user.name} : {bot.user.id}\n-----")
24
25@bot.event
26async def on_message(message):
27 await bot.handler.propagate(message)
28 await bot.process_commands(message)
29
30if __name__ == "__main__":
31 bot.run("Bot Token")
Custom Punishments¶
1from discord.ext import commands
2
3from antispam import AntiSpamHandler, Options
4from antispam.plugins import AntiSpamTracker
5
6bot = commands.Bot(command_prefix="!")
7bot.handler = AntiSpamHandler(bot, options=Options(no_punish=True, message_duplicate_count=3))
8bot.tracker = AntiSpamTracker(bot.handler, 5) # 5 Being how many 'punishment requests' before is_spamming returns True
9bot.handler.register_plugin(bot.tracker)
10
11
12@bot.event
13async def on_ready():
14 # On ready, print some details to standard out
15 print(f"-----\nLogged in as: {bot.user.name} : {bot.user.id}\n-----")
16
17
18@bot.event
19async def on_message(message):
20 await bot.handler.propagate(message)
21
22 if await bot.tracker.is_spamming(message):
23 # Insert code to mute the user
24
25 # Insert code to tell admins
26
27 # ETC
28 bot.tracker.remove_punishments(message)
29
30 await bot.process_commands(message)
31
32if __name__ == "__main__":
33 bot.run("Bot Token")