Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spam upgrade feature #13

Merged
merged 6 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -572,5 +572,5 @@ min-public-methods=2

# Exceptions that will emit a warning when being caught. Defaults to
# "BaseException, Exception".
overgeneral-exceptions=BaseException,
Exception
overgeneral-exceptions=builtins.BaseException,
builtins.Exception
71 changes: 42 additions & 29 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import help_command
import regrade
import utils
import spam

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'

Expand All @@ -47,6 +48,7 @@
BOT_VERSION=os.getenv('VERSION')
print(BOT_VERSION)
Test_bot_application_ID = int(os.getenv('TEST_BOT_APP_ID'))
guild_id = int(os.getenv('TEST_GUILD_ID')) ## needed for spam detection

TESTING_MODE = None

Expand All @@ -63,7 +65,6 @@ async def on_ready():
''' run on bot start-up '''
global TESTING_MODE
TESTING_MODE = False

#DiscordComponents(bot)
db.connect()
db.mutation_query('''
Expand Down Expand Up @@ -121,15 +122,25 @@ async def on_ready():
is_active BOOLEAN NOT NULL CHECK (is_active IN (0, 1))
)
''')

db.mutation_query('''
CREATE TABLE IF NOT EXISTS spam_settings (
warning_num INT,
timeout_num INT,
timeout_min INT,
timeout_hour INT,
timeout_day INT,
time_between_clears INT
)
''')
event_creation.init(bot)
office_hours.init(bot)
await cal.init(bot)
spam.init(bot) #initialize the spam function of the bot so spam.py has
# access to the bot and clearing starts
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')

await cal.init(bot) ##this needed to be moved below bc otherwise the stuff above is never called
###########################
# Function: on_guild_join
# Description: run when a the bot joins a guild
Expand Down Expand Up @@ -222,32 +233,23 @@ async def on_member_remove(member):
@bot.event
async def on_message(message):
''' run on message sent to a channel '''
#spam detection

url_data=[]
message_links = []
temp=[]
ctx = await bot.get_context(message)
print(message.content)
print(message.author.id)
count = 0
with open("spam.txt", "a",encoding='utf-8') as f:
f.writelines(f"{str(message.author.id)}\n")

with open("spam.txt","r+",encoding='utf-8') as f:
for line in f:
if line.strip("\n") == str(message.author.id):
count = count+1

if count>5:
#await ctx.send("spam;too many messages") --> this feature is commented out right now because it
#litterly tells you that you've sent too many messages every 5 messages you send, which is rarely even spam
f.truncate(0)
member = message.guild.get_member(message.author.id)
instructor = False
for role in member.roles:
if role.name == 'Instructor':
instructor = True
if not instructor:
# Only spam detect on non instructors
await spam.handle_spam(message, ctx, guild_id) # handles spam

# allow messages from test bot
print(message.author.bot)
print(message.author.id)
print(Test_bot_application_ID)
#print(message.author.bot)
#print(message.author.id)
#print(Test_bot_application_ID)
if message.author.bot and message.author.id == Test_bot_application_ID:
ctx = await bot.get_context(message)
await bot.invoke(ctx)
Expand Down Expand Up @@ -395,6 +397,21 @@ async def create_event(ctx):
''' run event creation interface '''
await event_creation.create_event(ctx, TESTING_MODE)

###########################
# Function: create_event
# Description: command to create event and send to event_creation module
# Ensures command author is Instructor
# Inputs:
# - ctx: context of the command
# Outputs:
# - Options to create event
###########################
@bot.command(name='set_spam_settings', help='Allows instructor to set spam settings')
@commands.has_role('Instructor')
async def set_spam_settings(ctx):
''' run spam setting prompts '''
await spam.set(ctx)

###########################
# Function: oh
# Description: command related office hour and send to office_hours module
Expand Down Expand Up @@ -688,7 +705,7 @@ async def update_chart(storage, name, link):
async def show_stats(ctx):
embed = Embed(title="Bot stats",
colour=ctx.author.colour,
thumbnail=bot.user.avatar_url,
#thumbnail=bot.user.avatar_url,
timestamp=datetime.utcnow())

proc = Process()
Expand Down Expand Up @@ -954,9 +971,7 @@ async def begin_tests(ctx):
if 'office-hour-test' in ch.name), None)
if test_oh_chan:
await office_hours.close_oh(ctx.guild, 'test')

await office_hours.open_oh(ctx.guild, 'test')

###########################
# Function: end_tests
# Description: Finalize automated testing
Expand All @@ -968,9 +983,7 @@ async def end_tests(ctx):
''' end tests command '''
if ctx.author.id != Test_bot_application_ID:
return

await office_hours.close_oh(ctx.guild, 'test')

# TODO maybe use ctx.bot.logout()
await ctx.bot.close()
# quit(0)
Expand Down
5 changes: 2 additions & 3 deletions src/cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ def update_calendar(ctx):
async def init(b):
''' initialize the calendar '''
global BOT

BOT = b
for guild in BOT.guilds:
for channel in guild.text_channels:
Expand All @@ -126,7 +125,7 @@ async def init(b):
await display_events(channel)
#close calls on assignments and exams
await closecalls.start(channel)

print('I dont get here')



Expand Down Expand Up @@ -211,9 +210,9 @@ async def closecalls(ctx): # pragma: no cover
if not MSG_CC_NONE:
MSG_CC_NONE = await ctx.send(embed=CLOSE_CALL_EMBED_NONE)
else:

# otherwise, edit the saved message from earlier
await MSG_CC_NONE.edit(embed=CLOSE_CALL_EMBED_NONE)

@closecalls.before_loop
async def before(): # pragma: no cover
await BOT.wait_until_ready()
30 changes: 20 additions & 10 deletions src/event_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import datetime
from datetime import timedelta
from discord.ui import Button, Select, View
from discord import SelectOption, ButtonStyle, Interaction
import validators
from discord import SelectOption, ButtonStyle
from discord.utils import get
from discord.ext import tasks, commands
from discord.ext import tasks

import validators

from utils import EmailUtility


import office_hours
import cal
import db
Expand Down Expand Up @@ -50,11 +53,13 @@ def check(m):
button_clicked = (await BOT.wait_for('button_click')).custom_id
"""
async def button_callback(interaction):
await interaction.response.send_message('What would you like the assignment to be called')
await interaction.response.send_message('What would you like the assignment to '
'be called')
msg = await BOT.wait_for('message', timeout = 60.0, check = check)
title = msg.content.strip()

await ctx.send('What is the due date of this assignment?\nEnter in format `MM-DD-YYYY`')
await ctx.send('What is the due date of this assignment?\nEnter in format '
'`MM-DD-YYYY`')
msg = await BOT.wait_for('message', timeout = 60.0, check = check)
date = msg.content.strip()
try:
Expand Down Expand Up @@ -130,7 +135,8 @@ async def project_button_callback(interaction):
)
await ctx.send('Project successfully created!')
await cal.display_events(ctx) ## needed so the calander updates
button3.callback = project_button_callback #assigns the project button to the function directly above
button3.callback = project_button_callback #assigns the project button to the function
# directly above

async def exam_button_callback(interaction):
await interaction.response.send_message('What is the title of this exam?')
Expand Down Expand Up @@ -240,7 +246,8 @@ async def office_hour_button_callback(interaction):
day_view.add_item(day_select)
#function to respond to instructor selection
async def instructor_select_callback(interaction):
instructor = instructor_select.values[0] ## assigns instructor with the selected instructor
instructor = instructor_select.values[0] ## assigns instructor with the selected
# instructor
print(instructor)
await interaction.response.send_message(
'Which day would you like the office hour to be on?',
Expand All @@ -257,7 +264,8 @@ async def instructor_select_callback(interaction):
async def day_select_callback(interaction):
day = day_select.values[0]
print(day)
await interaction.response.send_message('What is the start time of the office hour?\nEnter in 24-hour format' +
await interaction.response.send_message('What is the start time of the office '
'hour?\nEnter in 24-hour format' +
' e.g. an starting at 1:59pm can be inputted as 13:59')
msg = await BOT.wait_for('message', timeout = 60.0, check = check)
t_start = msg.content.strip()
Expand Down Expand Up @@ -291,8 +299,10 @@ async def day_select_callback(interaction):
)

await ctx.send('Office hour successfully created!')
await cal.display_events(ctx) ## updates the calender channel on discord with new office hours
day_select.callback = day_select_callback ## sets the day selection button to call to the correct function
await cal.display_events(ctx) ## updates the calender channel on discord with
# new office hours
day_select.callback = day_select_callback ## sets the day selection button to call to
# the correct function
button4.callback = office_hour_button_callback
else:
await ctx.author.send('`!create` can only be used in the `instructor-commands` channel')
Expand Down
2 changes: 1 addition & 1 deletion src/qna.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async def question(ctx, qs):
###########################
async def answer(ctx, num, ans):
''' answer the specific question '''
if int(num) not in QNA.keys():
if int(num) not in QNA:
await ctx.author.send('Invalid question number: ' + str(num))
# delete user msg
await ctx.message.delete()
Expand Down
Loading