Project 02 - Chatbot

Week 1 - A Place to Talk with Bots - Discord Bot

Step 1: Make an account on Repl.it and finish the set up, add the Discord Bot Token to Repl.it project

Step 2: Create the UI, Buttons, etc

@discord.ui.button(label="Hot Pot!")

async def button_callback(self, interaction, button):

await interaction.response.send_message("I don't like it!")

@discord.ui.button(label="Salad!")

async def button_callback2(self, interaction, button):

await interaction.response.send_message("I don't like it too!")

Create diagram and decision tree

Preview:

Tag @Creative Tech Ophelia bot, will shows “Do not ping me!”

Step 3: Create Response

@discord.ui.button(label="Hot Pot!")

async def button_callback(self, interaction, button):

await interaction.response.send_message("I don't like it!")

@discord.ui.button(label="Salad!")

async def button_callback2(self, interaction, button):

await interaction.response.send_message("I don't like it too!")

Week 2-3 - Algorithm: Binary tree, guessing bot

Step 1: Refer to the guessing bot template

Step 2: Set secrete

  1. tools → secrete → DISCORD_BOT_SECRET = “your bot secret” (recall: last week about how to get bot secrets)

Step 3: Algorithm: Binary tree

Step 4: Initialize nodes

class Node:

def __init__(self, question, answer=None, children=None):

self.question = question

self.answer = answer

self.children = children if children else []

node1 = Node('Pizza', answer='Yes')

node2 = Node('Burger', answer='No')

node3 = Node('Do you want something fast?', children=[node1, node2])

node4 = Node('Salad', answer='Yes')

node5 = Node('Soup', answer='No')

root = Node('Do you want something warm?', children=[node3, node4, node5])

Step 5: Create button + interaction

class GuessOptionView(View):

def __init__(self, node):

super().__init__()

if node.children:

for child in node.children:

self.add_item(GuessButton(child))

else:

self.add_item(Button(label=node.answer, custom_id=node.answer))

async def handleButtonPress(self, interaction, node):

if not node.children:

await interaction.response.send_message(content=f'Do you want {node.question}?', view=WrongView(node))

else:

await interaction.response.send_message(content=node.question, view=GuessOptionView(node))

class GuessButton(Button):

def __init__(self, node):

super().__init__(label=node.question, custom_id=node.answer)

self.node = node

async def callback(self, interaction):

await self.view.handleButtonPress(interaction, self.node)

Preview:

Click “Hot Pot!”

Shows “I don’t like it!”

Preview:

Click “Salad!”

Shows “I don’t like it too!”

Step 4: Full Code

import os

import discord

from discord.ui import View

intents = discord.Intents.default()

intents.message_content = True

client = discord.Client(intents=intents)

class AnotherView(View):

def __init__(self):

super().__init__()

@discord.ui.button(label="Hot Pot!")

async def button_callback(self, interaction, button):

await interaction.response.send_message("I don't like it!")

@discord.ui.button(label="Salad!")

async def button_callback2(self, interaction, button):

await interaction.response.send_message("I don't like it too!")

@client.event

async def on_ready():

print("Bot is ready!")

@client.event

async def on_message(message):

if message.author == client.user:

return

if client.user in message.mentions:

ourview = AnotherView()

await message.channel.send("Do not ping me!", view=ourview)

if message.content.startswith("$basicbot"):

ourview = AnotherView()

await message.channel.send("What to eat for lunch?", view=ourview)

my_secret = os.environ['DISCORD_BOT_SECRET']

client.run(my_secret)