Skip to content

Commit

Permalink
team broadcasts
Browse files Browse the repository at this point in the history
  • Loading branch information
96-LB committed Dec 16, 2020
1 parent 69c8d68 commit 121e916
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
16 changes: 8 additions & 8 deletions cogs/cogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,32 @@ async def unload(self, ctx, cog=None):
if not cog:
await ctx.send(f'You must specify a command group to unload!')
elif len([i for i in cog if i.isalpha()]) != len(cog):
await ctx.send(f'Command group names can only contain letters.')
await ctx.send(f'Command group names can only contain letters!')
else:
try:
self.bot.unload_extension(f'cogs.{cog}')
await ctx.send(f'Command group `{cog}` successfully unloaded.')
await ctx.send(f'Command group `{cog}` successfully unloaded!')
except commands.ExtensionNotLoaded:
await ctx.send(f'Command group `{cog}` has not yet been loaded.')
await ctx.send(f'Command group `{cog}` has not yet been loaded!')
except commands.ExtensionNotFound:
await ctx.send(f'Command group `{cog}` not found.')
await ctx.send(f'Command group `{cog}` not found!')

@organizer_channel()
@commands.command()
async def load(self, ctx, cog=None):
if not cog:
await ctx.send(f'You must specify a command group to unload!')
elif len([i for i in cog if i.isalpha()]) != len(cog):
await ctx.send(f'Command group names can only contain letters.')
await ctx.send(f'Command group names can only contain letters!')
else:
try:
self.bot.load_extension(f'cogs.{cog}')
await ctx.send(f'Command group `{cog}` successfully loaded.')
await ctx.send(f'Command group `{cog}` successfully loaded!')
except commands.ExtensionAlreadyLoaded:
self.bot.reload_extension(f'cogs.{cog}')
await ctx.send(f'Command group `{cog}` successfully reloaded.')
await ctx.send(f'Command group `{cog}` successfully reloaded!')
except commands.ExtensionNotFound:
await ctx.send(f'Command group `{cog}` not found.')
await ctx.send(f'Command group `{cog}` not found!')

@organizer_channel()
@commands.command()
Expand Down
4 changes: 3 additions & 1 deletion cogs/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ def __init__(self, bot):
def find_team(self, user):
return load_cog(self.bot, 'Teams').find_team(user)

async def broadcast(self, team, msg):
async def broadcast(self, team, msg, skip=None):
for user_id in team['USERS'].split('|'):
if user_id == str(skip):
continue
user = self.bot.get_user(int(user_id))
channel = user.dm_channel or await user.create_dm()
await channel.send(msg)
Expand Down
10 changes: 5 additions & 5 deletions cogs/novice.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def find_submission(self, submission_id):
def find_team(self, user):
return load_cog(self.bot, 'Teams').find_team(user)

def broadcast(self, team, msg):
return load_cog(self.bot, 'Messages').broadcast(team, msg)
def broadcast(self, team, msg, skip=None):
return load_cog(self.bot, 'Messages').broadcast(team, msg, skip)

@commands.dm_only()
@commands.command()
Expand Down Expand Up @@ -79,9 +79,9 @@ async def submit(self, ctx, problem=None):
'VERDICT': '-'
})
self.update()

await ctx.send(f'Submission received! We will judge your program soon. Your submission ID is `{submission_id}`.')

await self.broadcast(team, f'{ctx.author.mention} sent a submission (Submission ID: `{submission_id}`) to problem `{problem}`.', ctx.author.id)

@organizer_channel()
@commands.command()
Expand Down Expand Up @@ -118,7 +118,7 @@ async def judge(self, ctx, submission_id=None, verdict=None):
pieces[1] = f'diff\n+ Verdict: {verdict.upper()}\n'
await message.edit(content='```'.join(pieces))

await ctx.send('Verdict successfully submitted.')
await ctx.send('Verdict successfully submitted!')

def setup(bot):
bot.add_cog(Novice(bot))
22 changes: 16 additions & 6 deletions cogs/teams.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import core.data as data
from core.util import new_id
from core.util import new_id, load_cog
from discord.ext import commands

class Teams(commands.Cog):
Expand All @@ -17,6 +17,9 @@ def find_team(self, user):
return team
return None

def broadcast(self, team, msg, skip=None):
return load_cog(self.bot, 'Messages').broadcast(team, msg, skip)

@commands.dm_only()
@commands.group()
async def team(self, ctx):
Expand Down Expand Up @@ -53,26 +56,28 @@ async def create(self, ctx, division=None, *, name=None):
'TYPE': division.upper(),
'USERS': str(ctx.author.id)
})
await ctx.send(f'Welcome to QuHacks 2020! Your team has been registered under the name `{name}`! Your teammates can use the following team id to join: `{team_id}`. Best of luck!')
await ctx.send(f'Welcome to QuHacks 2020! Your team has been registered under the name `{name}`! Your teammates can use the following team ID to join: `{team_id}`. Best of luck!')
self.update()

@team.command()
async def join(self, ctx, team_id=None):
if self.find_team(ctx.author.id):
await ctx.send('You are already on a team! Use the `q!team leave` command if you want to switch teams.')
elif not team_id:
await ctx.send('You must specify a team id to join! You can get the id from the member who created the team.')
await ctx.send('You must specify a team ID to join! You can get the ID from the member who created the team.')
else:
team = self.find_team(team_id)
if not team:
await ctx.send('No team with that id was found. Try creating a team with the `q!team create` command.')
await ctx.send('No team with that IO was found. Try creating a team with the `q!team create` command.')
elif len(team['USERS'].split('|')) >= 4:
await ctx.send('Sorry, that team is full! Teams can only have up to 4 members.')
else:
team['USERS'] += f'|{ctx.author.id}'
name = team['NAME']
await ctx.send(f'Welcome to QuHacks 2020! You have successfully joined the team `{name}`. Best of luck!')
self.update()

await self.broadcast(team, f'{ctx.author.mention} has just joined your team!', ctx.author.id)
await ctx.send(f'Welcome to QuHacks 2020! You have successfully joined the team `{name}`. Use the `q!team` command to see more. Best of luck!')

@team.command()
async def leave(self, ctx):
Expand All @@ -84,11 +89,16 @@ async def leave(self, ctx):
users.remove(str(ctx.author.id))
if len(users) > 0:
team['USERS'] = '|'.join(users)
self.update()

await self.broadcast(team, f'{ctx.author.mention} has just left your team!', ctx.author.id)
await ctx.send('You have successfully left your team. Make sure to create or join a new team to participate in QuHacks 2020!')
else:
self.teams.remove(team)
self.update()

await ctx.send('You have successfully left your team! Since no more members remain, the team has been deleted, and you must create a new one to rejoin.')
self.update()


def setup(bot):
bot.add_cog(Teams(bot))
3 changes: 1 addition & 2 deletions core/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@

@bot.event
async def on_ready():
await bot.change_presence(activity=discord.Game(name='loading cogs...'))
for cog in [cog.replace('.py', '') for cog in os.listdir('cogs') if '.py' in cog]:
await bot.change_presence(activity=discord.Game(name=f'loading cogs.{cog}...'))
print(f'Loading cogs.{cog}...')
try:
bot.load_extension(f'cogs.{cog}')
except Exception as e:
await bot.change_presence(activity=discord.Game(name=f'error loading cogs.{cog}!'))
print(f'Error loading cogs.{cog}!')
raise e
print('FreddyBot is running!')
Expand Down

0 comments on commit 121e916

Please sign in to comment.