run
1import sys 2from pathlib import Path # if you haven't already done so 3 4file = Path(__file__).resolve() 5parent, root = file.parent, file.parents[1] 6sys.path.append(str(root)) 7 8# Additionally remove the current file's directory from sys.path 9try: 10 sys.path.remove(str(parent)) 11except ValueError: # Already removed 12 pass 13 14import os 15import discord 16from dotenv import load_dotenv 17import src.checkers.profanity 18from src.checkers.apology import ApologyChecker 19from src.checkers.reporter import ReportChecker 20from src.utils.msg import clean_message, get_msg_template, get_help_message 21from src.checkers.bully import BullyChecker 22 23intents = discord.Intents.default() 24intents.message_content = True 25client = discord.Client(intents=intents) 26 27 28@client.event 29async def on_ready(): 30 print(f"{client.user} has connected to Discord!") 31 32 33@client.event 34async def on_message(message): 35 """ 36 :param message: The context of the message sent on Discord 37 :return: None 38 """ 39 print(f'Message from {message.author}: {message.content}: {message.channel.name}') 40 # Step 0: check if message is by Bot 41 author = message.author 42 aid = message.author.id 43 channel_name = message.channel.id 44 if author == client.user: 45 return 46 # Step 1: Pre-process message 47 msg_content = clean_message(str(message.content)) 48 if msg_content.find('hello') != -1: 49 await message.reply("Hey <@{0}> how's it going?".format(aid)) 50 if msg_content.find('help') != -1: 51 await message.reply(get_help_message()) 52 # Step 2: Check for profanity 53 if pc.check_message(channel_name, msg_content): 54 # Step2.1 : Checking if the user has a first time offense 55 warning = ac.check_user_for_warning(aid, channel_name) 56 await message.channel.send(get_msg_template(aid, "profanity", warning)) 57 # Step2.2: Banning user if not a first-time offense 58 ac.add_warning(aid, channel_name) 59 if not warning: 60 await message.author.ban() 61 62 # Step 3: Check for Bully & Toxic Traits 63 traits = bc.check_message(msg_content) 64 if len(traits) > 0: 65 # Step3.1 : Checking if the user has a first time offense 66 warning = ac.check_user_for_warning(aid, channel_name) 67 await message.channel.send(get_msg_template(aid, traits, warning)) 68 # Step3.2: Banning user if not a first-time offense 69 ac.add_warning(aid, channel_name) 70 if not warning: 71 await message.author.ban() 72 73 # Step 4: Check for Apology 74 if ac.check_message(msg_content): 75 if ac.add_apology(aid, channel_name): 76 await message.reply("Hey <@{0}>, your apology is accepted by the bot".format(aid)) 77 78 # Step 5: Reporting a Profane Word 79 if rc.check_message(msg_content): 80 report_type, report_token = rc.parse_message(msg_content) 81 if report_type == "word": 82 if pc.add_words(channel_name, report_token): 83 await message.reply("{0} has been added as a toxic word".format(report_token)) 84 85if __name__ == "__main__": 86 load_dotenv("bot.env") 87 token = os.getenv("token") 88 bc = BullyChecker() 89 ac = ApologyChecker() 90 pc = src.checkers.profanity.ProfanityChecker() 91 rc = ReportChecker() 92 client.run(token)
@client.event
async def
on_ready():
@client.event
async def
on_message(message):
34@client.event 35async def on_message(message): 36 """ 37 :param message: The context of the message sent on Discord 38 :return: None 39 """ 40 print(f'Message from {message.author}: {message.content}: {message.channel.name}') 41 # Step 0: check if message is by Bot 42 author = message.author 43 aid = message.author.id 44 channel_name = message.channel.id 45 if author == client.user: 46 return 47 # Step 1: Pre-process message 48 msg_content = clean_message(str(message.content)) 49 if msg_content.find('hello') != -1: 50 await message.reply("Hey <@{0}> how's it going?".format(aid)) 51 if msg_content.find('help') != -1: 52 await message.reply(get_help_message()) 53 # Step 2: Check for profanity 54 if pc.check_message(channel_name, msg_content): 55 # Step2.1 : Checking if the user has a first time offense 56 warning = ac.check_user_for_warning(aid, channel_name) 57 await message.channel.send(get_msg_template(aid, "profanity", warning)) 58 # Step2.2: Banning user if not a first-time offense 59 ac.add_warning(aid, channel_name) 60 if not warning: 61 await message.author.ban() 62 63 # Step 3: Check for Bully & Toxic Traits 64 traits = bc.check_message(msg_content) 65 if len(traits) > 0: 66 # Step3.1 : Checking if the user has a first time offense 67 warning = ac.check_user_for_warning(aid, channel_name) 68 await message.channel.send(get_msg_template(aid, traits, warning)) 69 # Step3.2: Banning user if not a first-time offense 70 ac.add_warning(aid, channel_name) 71 if not warning: 72 await message.author.ban() 73 74 # Step 4: Check for Apology 75 if ac.check_message(msg_content): 76 if ac.add_apology(aid, channel_name): 77 await message.reply("Hey <@{0}>, your apology is accepted by the bot".format(aid)) 78 79 # Step 5: Reporting a Profane Word 80 if rc.check_message(msg_content): 81 report_type, report_token = rc.parse_message(msg_content) 82 if report_type == "word": 83 if pc.add_words(channel_name, report_token): 84 await message.reply("{0} has been added as a toxic word".format(report_token))
Parameters
- message: The context of the message sent on Discord
Returns
None