Skip to content

Instantly share code, notes, and snippets.

@Restioson
Created April 23, 2017 14:06
Show Gist options
  • Save Restioson/812cc3ec6b7e8f7e16ccc120aa811b7c to your computer and use it in GitHub Desktop.
Save Restioson/812cc3ec6b7e8f7e16ccc120aa811b7c to your computer and use it in GitHub Desktop.
# -- Commands -- #
async def command_meme(self, keywords: list, channel: discord.Channel):
"""Sends closest matching meme to channel"""
matching = []
Meme = collections.namedtuple("Meme", ["matches", "filename", "path"])
# Populate list of matching memes with `Meme` named tuples
for meme_path in self.memes:
meme_filename = meme_path.split(os.path.sep)[-1]
matches = filename_matches(keywords, meme_filename)
if matches > 0:
matching.append(Meme(matches, meme_filename, meme_path))
# Sort matching
matching.sort(key=lambda x: x.matches)
matching.reverse() # Make item with highest matches first instead of last
# Meme with most matches
matching_meme = matching[0] if len(matching) > 0 else None
# Send the meme with the most matches
if matching_meme:
print("Sending meme \"{0}\"...".format(matching_meme.filename))
try:
await self.send_file(channel, matching[0].path)
print("... meme \"{0}\" successfully sent!".format(matching_meme.filename))
# Catch any exceptions from discord, e.g no permission, file too large
except discord.DiscordException:
print("Error sending meme \"{0}\": \n{1}".format(matching_meme.filename, traceback.format_exc()))
print("Was the file too large? Do you have permission to send files?")
# TODO make gui alert
else:
print("No matching memes") # TODO make gui alert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment