async def eval(self, user): member = self.server.get_member_named(user) if member is None: raise CommandFailure(f"User {bold(user)} doesn't exist!") # Check if user already has a birthday added all_birthdays = get_birthdays(BIRTHDAY_FILE) curr_date = find_user(all_birthdays, member.id) dm_today = datetime.datetime.today().strftime("%d/%m") bday_role = get_role(self.server) if curr_date is not None and curr_date != dm_today: raise CommandFailure( f"{bold(user)} already has a birthday set for " f"a different date - they don't need the date today") elif curr_date == dm_today: await self.client.add_roles(member, bday_role) raise CommandFailure( f"It's already {bold(user)}'s birthday! Attempting to give role again..." ) if curr_date is None: # User doesn't have any birthday set # Set their birthday to today for next year all_birthdays[dm_today].append(member.id) save_birthdays(all_birthdays) # Grant them the Birthday! role await self.client.add_roles(member, bday_role) return f"{bold(user)} has been granted the Birthday role."
def check_permissions(self): if self.roles_required is not None: for role in self.message.author.roles: if role.name.lower() in self.roles_required: return raise CommandFailure("You need to be a %s to use that command" % \ " or ".join(self.roles_required))
async def play_audio(self, file): channel = self.message.author.voice_channel if channel is None: raise CommandFailure( "You need to join a voice channel to use this command") if Command.audio_playing: raise CommandFailure("Can only play one audio file at once") Command.audio_playing = True # coroutines, no mutex required voice = await self.client.join_voice_channel(channel) player = voice.create_ffmpeg_player('files/' + file) player.start() duration = MP3('files/' + file).info.length await asyncio.sleep(duration) await voice.disconnect() Command.audio_playing = False
def eval(self, name): user = self.from_name(name) if user is None: raise CommandFailure("User not found") tags = Tag.select_or_err(lambda x: x.user == int(user.id), "User has no tags") return EmbedTable(fields=['Platform', 'Tag'], table=[(x.platform.title(), x.tag) for x in tags], colour=self.EMBED_COLOR, user=user, title="Tags for " + bold(user.name))
def eval(self): all_birthdays = get_birthdays(BIRTHDAY_FILE) # Check if they've given their birthday curr_date = find_user(all_birthdays, self.user) if curr_date is None: raise CommandFailure("You haven't supplied your birthday.") # Remove it and save all_birthdays[curr_date].remove(self.user) save_birthdays(all_birthdays) return "Your birthday has been removed."
def eval(self, birthday): dt_birthday = validate(birthday) if dt_birthday is None: raise CommandFailure("Please input a valid date format (dd/mm).") all_birthdays = get_birthdays(BIRTHDAY_FILE) # Check if they've already given their birthday curr_date = find_user(all_birthdays, self.user) if curr_date is not None: raise CommandFailure( "You've already entered your birthday. " "If you wish to change it, please remove it first.") # Convert datetime object back to a consistent dd/mm string day_month = dt_birthday.strftime("%d/%m") # Add it and save all_birthdays[day_month].append(self.user) save_birthdays(all_birthdays) return f"{dt_birthday:%-d} {dt_birthday:%B} has been added as your birthday!"
def eval(self): all_birthdays = get_birthdays(BIRTHDAY_FILE) # Check if they've given their birthday curr_date = find_user(all_birthdays, self.user) if curr_date is None: raise CommandFailure("You haven't supplied your birthday.") all_birthdays[curr_date].remove(self.user) with open(BIRTHDAY_FILE, "w") as birthdays: json.dump(all_birthdays, birthdays) return "Your birthday has been removed."
def eval(self, course_code): if re.search(r'^[a-zA-Z]{4}[0-9]{4}$', course_code) is None: raise CommandFailure( f'Incorrectly formatted course code: {bold(course_code)}') course = subject_details(course_code) if course is None: return f'Course {course_code} could not be found' ret = Embed(title=course['title'], description=course['description'], url=course['link'], color=self.EMBED_COLOR) ret.add_field(name='Offering Terms', value=course['offerings']) ret.add_field(name='Enrolment Conditions', value=course['conditions']) return ret
def eval(self, name, platform, tag): user = self.from_name(name) if user is None: raise CommandFailure("User not found") warning = '' if not select(count(t) for t in Tag if t.platform == platform.lower())[:][0]: platforms = ', '.join(sorted(select(t.platform for t in Tag)[:])) warning = bold( 'WARNING: creating a new platform. Please check that the platform doesn\'t already ' 'exist by another name.\n' ) + 'Current platforms are ' + platforms + '\n' Tag.create_or_update(user=int(user.id), platform=platform.lower(), tag=tag) return "%s%s added as %s tag for %s" % (warning, tag, platform.title(), user.name)
def select_or_err(cls, fn, err=None): objs = cls.select(fn) if not objs: raise CommandFailure(cls.err if err is None else err) return objs
def get_or_err(cls, err=None, **kwargs): obj = cls.get(**kwargs) if obj is None: raise CommandFailure(cls.err if err is None else err) return obj
def eval(self, name, platform): user = self.from_name(name) if user is None: raise CommandFailure("User not found") Tag.delete_or_err(user=int(user.id), platform=platform.lower()) return "%s tag for %s removed" % (platform.title(), user.name)