Discord Bot Practice


Feb. 2024
Personal project
-

< Discord, Phython >








Process documentation




1. Add reaction



if message.content.startswith("I'm bored"):
    ourview = MyCustomView()
    await message.channel.send("Oh, what do you want me to do?", view=ourview)
    await message.add_reaction("🥲")







2. Tried to add image


@discord.ui.button(label="I'm bad at drawing")
async def buttonB_callback(self, interaction, button):

urllib.request.urlretrieve("https://hips.hearstapps.com/hmg-prod/images/dog-puppy-on-garden-royalty-free-image-1586966191.jpg?crop=0.752xw:1.00xh;0.175xw,0&resize=1200:*, Cutedog.jpg")

 image = discord.File("Cutedog.jpg", filename="Cutedog.jpg")

 embed = discord.Embed(title="Cute dog", description="Hope you feel better", color=0x00ff56)

 embed.set_thumbnail(url="https://hips.hearstapps.com/hmg-prod/images/dog-puppy-on-garden-royalty-free-image-1586966191.jpg?crop=0.752xw:1.00xh;0.175xw,0&resize=1200:*")
 embed.add_field(name="Cutedog.jpg", value="1", inline=True)

 await channel.send(embed=embed, file=image)


I tried to store an image inserver before loading on to the screen. But it didn’t work.










 @discord.ui.button(label="I'm bad at drawing")
async def buttonB_callback(self, interaction, button):
 embed=discord.Embed(title="Cute dog image", url="https://hips.hearstapps.com/hmg-prod/images/dog-puppy-on-garden-royalty-free-image-1586966191.jpg?crop=0.752xw:1.00xh;0.175xw,0&resize=1200:*", description="Hope you feel better")

 embed.set_author(name="Bot", url="https://hips.hearstapps.com/hmg-prod/images/dog-puppy-on-garden-royalty-free-image-1586966191.jpg?crop=0.752xw:1.00xh;0.175xw,0&resize=1200:*", icon_url="https://hips.hearstapps.com/hmg-prod/images/dog-puppy-on-garden-royalty-free-image-1586966191.jpg?crop=0.752xw:1.00xh;0.175xw,0&resize=1200:*")

embed.set_thumbnail(url="https://hips.hearstapps.com/hmg-prod/images/dog-puppy-on-garden-royalty-free-image-1586966191.jpg?crop=0.752xw:1.00xh;0.175xw,0&resize=1200:*")

 embed.add_field(name="Cute dog image", value="2", inline=True)

 await interaction.response.send_message(embed=embed)


So I removed urllib and only used absic embed function. Then it worked:)

--------------------------------------------------------------------------------

embed.set_thumbnail(url="https://hips.hearstapps.com/hmg-prod/images/dog-puppy-on-garden-royalty-free-image-1586966191.jpg?crop=0.752xw:1.00xh;0.175xw,0&resize=1200:*")
# embed.add_field(name="Cute dog image", value="2", inline=True)


Then, I also removed the unnecessary parts.



3. Add reaction while user is writing




@client.event
async def on_typing(channel, user, when):
await channel.send(str(user) + "Yeah, good. Bring it on!")
return


I wanted to give impression to users that the bot is hearing.






4. Bot trying its best




class MyCustomView(View):
def __init__(self):
super().__init__()

@discord.ui.button(label="I'm bad at jokes")
async def button_callback(self, interaction, button):
await interaction.response.send_message("What falls, but never needs a bandage?")

if message.content.startswith("I don't know"):
ourview = MyCustomView2()
await message.channel.send("🌧️", view=ourview)

class MyCustomView2(View):
def __init__(self):
super().__init__()

@discord.ui.button(label="I told you")
async def buttonE_callback(self, interaction, button):
await interaction.response.send_message("I did my best😤")


This is the reaction that comes out when a bot asks for a joke even though he says he's not good at it. When he answered that he didn't know the bot's joke, he let him know the answer and told him that he did his best.

To do this, I created MycustomView 2 and tried to get a second interaction to happen.






5. Changing the button color





@discord.ui.button(label="I'm bad at jokes", style=discord.ButtonStyle.primary)


I changed the color of the button. Originally, I wanted to change it to FAA619, which is the thumbnail index of the bot, but I found that the color you specified cannot be substituted into the button except for a few colors. So, I changed the color of the button using the colors already in place.






6. Add Youtube link and Dropdown



@discord.ui.button(label="What about some music", style=discord.ButtonStyle.success)
async def buttonC_callback(self, interaction, button):
embed = discord.Embed(
title="Music always helps when we feel bad",
description="[Hope this helps you to feel better](https://www.youtube.com/watch?v=ebudw5S30sA)",


I came up with a YouTube video link to recommend music for a change. I inserted the link using the embedded function again.

--------------------------------------------------------------------------------

class CustomSelect(Select):
def __init__(self):
options = [
discord.SelectOption(label="ttokppokki"),
discord.SelectOption(label="Buldak Fried Noodles"),
discord.SelectOption(label="Chicken and beer"),
discord.SelectOption(label="Pizza and beer"),
discord.SelectOption(label="Salad...?")
]

super().__init__(placeholder="I hope there's something you want to eat in here", options=options, min_values=1, max_values=2)

async def callback(self, interaction):
await interaction.response.send_message(f'{self.values[0]} sounds great!')


Also, I thought a drop-down would be better for the food recommendation feature, so I added it. The user made it more than one even, and the bot made it look like it was recommending food. (Actually, it suggests the first option)















































Seoul, South Korea