Skip to content

Instantly share code, notes, and snippets.

@iref
Last active April 18, 2018 19:34
Show Gist options
  • Save iref/c343aea18b91d314721ad0c569ee5a0e to your computer and use it in GitHub Desktop.
Save iref/c343aea18b91d314721ad0c569ee5a0e to your computer and use it in GitHub Desktop.
from discord.ext import commands
from bb8.embed import CardImage
from bb8.search import Search
from bb8.swdestinydb import SWDestinyDBClient
import logging
import os
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN")
db_client = SWDestinyDBClient()
search = Search(db_client)
class BB8(commands.Bot):
def __init__(self, extensions, search, **options):
super().__init__(**options)
self.initial_extensions = extensions
self.search = search
self.load_extensions()
def load_extensions(self):
for extension in self.initial_extensions:
self.load_extension(extension)
print(f'Loaded extension {extension}')
async def on_ready(self, *_):
print(f"Logged in as {self.user} with id {self.user.id}")
def main():
bot = BB8(
extensions=['bb8.cards'],
search=search,
command_prefix="!",
description="Provides useful commands for Star Wars Destiny fans."
)
bot.run(DISCORD_TOKEN)
if __name__ == "__main__":
main()
from discord.ext import commands
from .embed import CardImage
import re
class SWCardSearch:
# Regex to parse card mentions from regular messages.
PATTERN = r"\[\[(.+)\]\]"
def __init__(self, bot):
self.bot = bot
@commands.command(description="Search for card on Star Wars Destiny DB")
async def card(self, *, terms):
card = self.bot.search.find_card(terms)
if card:
embed = CardImage(card).render()
await self.bot.say(embed=embed)
async def on_message(self, message):
if message.author.bot:
return
queries = set(re.findall(SWCardSearch.PATTERN, message.content))
for query in queries:
card = self.bot.search.find_card(query)
if card:
await self.bot.send_message(message.channel, card["imagesrc"])
else:
await self.bot.send_message(message.channel, f"No card found. :(")
await self.bot.process_commands(message)
def setup(bot):
bot.add_cog(SWCardSearch(bot))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment