async def assign_peers(self, guild, cohort_data, day, date): # get & shuffle students role_students = get(guild.roles, name='Students') students = role_students.members random.shuffle(students) for i, _ in enumerate(students): # assign evaluator & evaluatee evaluator = students[i] evaluatee = students[i + 1] \ if i != len(students) - 1 else students[0] # get evaluatee data user_url = f'{API_URL}/users/{evaluatee.id}' user_r = s.get(user_url, timeout=10) user_data = user_r.json() chn_eval_id = next( channel['id'] for channel in user_data['channels'] if channel['cohort']['id'] == cohort_data['id'] ) chn_eval = get(guild.text_channels, id=chn_eval_id) # # deny other students permission to view evaluatee channel # for user in chn_eval.members: # if user in students and user.id != evaluatee.id: # await chn_eval.set_permissions(user, overwrite=None) if day != cohort_data['duration']: # update eval table eval_url = f'{API_URL}/evals' post_eval_data = { 'evaluator_id': evaluator.id, 'evaluatee_id': evaluatee.id, 'cohort_id': utils.active_cohort['id'], 'date': date } eval_r = s.post(eval_url, json=post_eval_data, timeout=10) if eval_r.status_code != requests.codes.created: eval_r.raise_for_status() # grant new tester permission to view evaluatee channel await chn_eval.set_permissions(evaluator, view_channel=True) # send message to evaluatee channel informing evaluation pairs eval_data = eval_r.json() eval_id = str(eval_data['id']).zfill(4) message = ( f'Hi {evaluatee.mention}, your reviewer for today is ' f'{evaluator.mention}, the two of you may use this ' 'channel to communicate.' ) await chn_eval.send(message)
async def addbot(self, ctx, link): """ Adds bot to server Args: link: Bot-invite link """ regex = ( r'https://discord.com/api/oauth2/authorize\?client_id=([0-9]{18})' r'&permissions=([0-9]+)&scope=bot') match = re.fullmatch(regex, link) if not match: raise commands.BadArgument bot_id = int(match.group(1)) perm = int(match.group(2)) if perm != BOT_PERM: raise self.WrongBotPermissions # get bot data get_url = f'{API_URL}/bots/{bot_id}' get_r = s.get(get_url, timeout=10) if get_r.status_code == requests.codes.ok: raise self.MultipleBotApplication elif get_r.status_code != requests.codes.not_found: get_r.raise_for_status() # notify user bot will be added to server await ctx.reply('Your bot will be added into the server soon.') # prompt '@Pyrates' to add student bot role_devs = get(ctx.guild.roles, name='Pyrates') chn_server_log = get(ctx.guild.text_channels, name='server-log') msg = await chn_server_log.send( f'{role_devs.mention} Kindly add {ctx.author.mention}\'s bot:\n' f'{link}') # update bot table post_url = f'{API_URL}/bots' post_data = { 'id': bot_id, 'user_id': ctx.author.id, 'cohort_id': utils.active_cohort['id'], 'msg_id': msg.id } post_r = s.post(post_url, json=post_data, timeout=10) if post_r.status_code != requests.codes.created: post_r.raise_for_status()
async def me(self, ctx): """ Returns user's basic information """ url = f'{API_URL}/users/{ctx.author.id}' r = s.get(url, timeout=10) if r.status_code != requests.codes.ok: r.raise_for_status() data = r.json() message = ( f'Your account was created at {data["created_at"]} and last ' f'updated at {data["last_updated"]}.') await ctx.reply(message)
async def givexp(self, ctx, user: discord.Member, xp: int = 10): """ Awards XP to a user Args: user: User to award XP xp(int): Optional argument XP amount """ # get user data url = f'{API_URL}/users/{user.id}' get_r = s.get(url, timeout=10) if get_r.status_code != requests.codes.ok: get_r.raise_for_status() # update user table data = get_r.json() put_data = {'xp': data['xp'] + xp} put_r = s.put(url, json=put_data, timeout=10) if put_r.status_code != requests.codes.ok: put_r.raise_for_status()
async def on_member_join(self, member): # send log to '#server-log' chn_server_log = get(member.guild.text_channels, name='server-log') await chn_server_log.send( '```diff\n' f'# {now()}\n' '\n' f'+ {member}: {member.id} joined the server.```') if member.bot: # update bot table bot_url = f'{API_URL}/bots/{member.id}' put_bot_data = { 'name': member.name, 'discriminator': member.discriminator, 'display_name': member.display_name } bot_r = s.put(bot_url, json=put_bot_data, timeout=10) if bot_r.status_code == requests.codes.not_found: return elif bot_r.status_code != requests.codes.ok: bot_r.raise_for_status() # assign '@Student Bots' role role_student_bots = get(member.guild.roles, name='Student Bots') await member.add_roles(role_student_bots) bot_data = bot_r.json() cohort_id = bot_data['cohort']['id'] # discord.py Botcamp if cohort_id in utils.dpy: user_id = bot_data['user']['id'] # get user data user_url = f'{API_URL}/users/{user_id}' user_r = s.get(user_url, timeout=10) if user_r.status_code != requests.codes.ok: user_r.raise_for_status() user_data = user_r.json() channels = user_data['channels'] chn_eval_id = next(channel['id'] for channel in channels if channel['cohort']['id'] == cohort_id) # grant bot permission to view user channel chn_eval = get(member.guild.text_channels, id=chn_eval_id) await chn_eval.set_permissions(member, read_messages=True) # send message to user channel informing bot entrance user = get(member.guild.members, id=user_id) message = ( f'Hi {user.mention}, you may interact with your bot ' f'{member.mention} here!') await chn_eval.send(message) # mark bot-invite message in '#server-log' as successful msg_id = bot_data['msg_id'] chn_server_log = get(member.guild.text_channels, name='server-log') msg = await chn_server_log.fetch_message(msg_id) await msg.add_reaction('🟢') else: # update user table user_url = f'{API_URL}/users/{member.id}' put_user_data = { 'name': member.name, 'discriminator': member.discriminator, 'display_name': member.display_name } user_r = s.put(user_url, json=put_user_data, timeout=10) if user_r.status_code == requests.codes.not_found: return elif user_r.status_code != requests.codes.ok: user_r.raise_for_status()
async def enrol(self, ctx, user: discord.Member, cohort_id: int = 0): """ Enrols user to a cohort Args: user: User to enrol cohort_id(int): Cohort ID, defaults to current active cohort """ # create user user_url = f'{API_URL}/users' post_user_data = { 'id': user.id, 'name': user.name, 'discriminator': user.discriminator, 'display_name': user.display_name } user_r = s.post(user_url, json=post_user_data, timeout=10) if user_r.status_code != requests.codes.created \ and user_r.status_code != requests.codes.conflict: user_r.raise_for_status() # create enrolment cohort_id = cohort_id if cohort_id != 0 else utils.active_cohort['id'] enrolment_url = f'{API_URL}/enrolments' post_enrolment_data = {'user_id': user.id, 'cohort_id': cohort_id} enrolment_r = s.post(enrolment_url, json=post_enrolment_data, timeout=10) if enrolment_r.status_code != requests.codes.created \ and enrolment_r.status_code != requests.codes.conflict: enrolment_r.raise_for_status() # assign '@Students' role role_students = get(ctx.guild.roles, name='Students') await user.add_roles(role_students) # discord.py Botcamp if cohort_id in utils.dpy: # create user channel role_dev_bot = get(ctx.guild.roles, name='Pyrate Bot') role_bocals = get(ctx.guild.roles, name='BOCALs') role_observers = get(ctx.guild.roles, name='Observers') role_digital_penang = get(ctx.guild.roles, name='Digital Penang') overwrites = { role_dev_bot: discord.PermissionOverwrite(read_messages=True), role_bocals: discord.PermissionOverwrite(read_messages=True), role_observers: discord.PermissionOverwrite(read_messages=True), # digital penang role_digital_penang: discord.PermissionOverwrite(read_messages=True), user: discord.PermissionOverwrite(read_messages=True), ctx.guild.default_role: discord.PermissionOverwrite(read_messages=False) } topic = 'Test your bot here!' ctg_eval = get(ctx.guild.categories, name='1-to-1 peer discussions') chn_eval = await ctg_eval.create_text_channel( user.name, overwrites=overwrites, topic=topic) channel_url = f'{API_URL}/channels' post_channel_data = { 'id': chn_eval.id, 'name': chn_eval.name, 'user_id': user.id, 'cohort_id': cohort_id } channel_r = s.post(channel_url, json=post_channel_data, timeout=10) if channel_r.status_code == requests.codes.conflict: await chn_eval.delete() elif channel_r.status_code != requests.codes.created: channel_r.raise_for_status() # assign cohort role if cohort_id == utils.C4T_DPY_ALPHA: role_name = 'C4T discord.py Botcamp (Alpha)' elif cohort_id == utils.C4T_DPY_BETA: role_name = 'C4T discord.py Botcamp (Beta)' elif cohort_id == utils.C4T_DPY_DEC2021: role_name = 'C4T discord.py Botcamp (Dec 2021)' elif cohort_id == utils.C4W_DPY_FEB2022: role_name = 'C4W discord.py Botcamp (Feb 2022)' elif cohort_id == utils.C4T_DPY_MAR2022: role_name = 'C4T discord.py Botcamp (Mar 2022)' elif cohort_id == utils.C4T_DPY_TEST: role_name = 'C4T discord.py Botcamp (Test)' role_cohort = get(ctx.guild.roles, name=role_name) await user.add_roles(role_cohort) # send command completion message if enrolment_r.status_code == requests.codes.created: enrolment_data = enrolment_r.json() message = (f'{user.mention} enrolled to ' f'{enrolment_data["cohort"]["name"]}.') else: cohort_url = f'{API_URL}/cohorts/{cohort_id}' cohort_r = s.get(cohort_url, timeout=10) if cohort_r.status_code != requests.codes.ok: cohort_r.raise_for_status() cohort_data = cohort_r.json() message = ( f'{user.mention} already enrolled to {cohort_data["name"]}.') await ctx.reply(message)