Fetch custom rewards from database rather than hard coded

This commit is contained in:
Terry Hearst 2022-10-15 17:16:19 -04:00
parent 8cfc10c039
commit 87c5fd1abf

20
bot.py
View file

@ -17,6 +17,7 @@ class Bot(commands.Bot):
self.db_cur = self.db_con.cursor() self.db_cur = self.db_con.cursor()
self.db_cur.execute("CREATE TABLE IF NOT EXISTS custom_commands(command TEXT, output_text TEXT, author TEXT)") self.db_cur.execute("CREATE TABLE IF NOT EXISTS custom_commands(command TEXT, output_text TEXT, author TEXT)")
self.db_cur.execute("CREATE TABLE IF NOT EXISTS channel_reward_messages(title TEXT, output_text TEXT)")
print("Created database table") print("Created database table")
commands = self.db_cur.execute("SELECT count(*) FROM custom_commands").fetchone() commands = self.db_cur.execute("SELECT count(*) FROM custom_commands").fetchone()
print(f"{commands[0]} custom commands available") print(f"{commands[0]} custom commands available")
@ -58,17 +59,14 @@ class Bot(commands.Bot):
# Can I put this function somewhere else LMAO # Can I put this function somewhere else LMAO
@self.event() @self.event()
async def event_pubsub_channel_points(event: pubsub.PubSubChannelPointsMessage): async def event_pubsub_channel_points(event: pubsub.PubSubChannelPointsMessage):
reward = event.reward.title reward_title = event.reward.title
channel = self.get_channel(os.environ["CHANNEL"]) result = self.db_cur.execute("SELECT output_text FROM channel_reward_messages WHERE title=?", [reward_title]).fetchone()
if reward == "nice": if result is not None:
await channel.send(f"@{event.user.name} nice") message = result[0]
elif reward == "very nice": message = message.replace("%USER%", event.user.name)
await channel.send(f"@{event.user.name} very nice") if event.input:
elif reward == "Add a custom command to my bot!": message = message.replace("%INPUT%", event.input)
await channel.send(f"{event.user.name} redeemed a custom command!") await self.get_channel(os.environ["CHANNEL"]).send(message)
await self.add_custom_command(channel, event.input, event.user.name)
else:
await channel.send(f"{event.user.name} redeemed {event.input}!")
async def event_message(self, message): async def event_message(self, message):
if message.echo: if message.echo: