--- title: DCbot week2 tags: 教學簡報 --- # Dc bot第二周 --- # 放圖片 ---- ```python= @bot.command() async def Pic(ctx): pic = discord.File('你的檔案路徑') await ctx.send(file = pic) ``` ---- # 想放網路圖片? ---- ```python= bot.command() async def web(ctx): await ctx.send("https://media.discordapp.net/attachments/678292614398869513/1060893685916643380/image.png?width=598&height=669") ``` ---- # 讓他有更多選擇 ---- ```python= async def web2(ctx): random_pic= random.choice(['https://media.discordapp.net/attachments/678292614398869513/1060893685916643380/image.png?width=598&height=669' ,'https://media.discordapp.net/attachments/678292614398869513/1062335654475878421/AF973719-5569-4803-896C-CA37F2B62679.jpg?width=502&height=669']) await ctx.send(random_pic) ``` ---- # GIF?? 一樣的道理 ---- ```python= @bot.command() async def web3(ctx): random_gif = random.choice(["https://media2.giphy.com/media/b9QBHfcNpvqDK/giphy.gif?cid=ecf05e47lz752v729gppaw7dc8l6y3uobxciem3cx2vg8nea&rid=giphy.gif&ct=g" , "https://media4.giphy.com/media/4EiGNSTfy4WC4/giphy.gif?cid=ecf05e47lbxfgj2jvxy7l7rn7xam4ikane4oeh48zv4y2u9f&rid=giphy.gif&ct=g"]) await ctx.send(random_gif) ``` --- # 讓他更酷一點(embed)鑲嵌圖片 ---- ```python= @bot.command() async def No(ctx, member:discord.Member = None): if member == None: member = ctx.author name = member.display_name pfp = member.display_avatar embed = discord.Embed(title="This is my embed", description="Its a very cool embed", colour=discord.Colour.random(), timestamp= None) embed.set_author(name=f"{name}") embed.set_thumbnail(url=f"{pfp}") embed.set_image(url="https://media.discordapp.net/attachments/678292614398869513/1060893685916643380/image.png?width=598&height=669") embed.add_field(name="This is 1 field", value="This field is just a value",inline=False) embed.add_field(name="This is 2 field", value="This field is just a value",inline=True) embed.add_field(name="This is 3 field", value="This field is just a value",inline=False) embed.set_footer(text=f"{name} here",icon_url="https://media.discordapp.net/attachments/678292614398869513/1060893685916643380/image.png?width=598&height=669") await ctx.send(embed = embed) ``` --- # button ---- ```python= @bot.event#機器人啟動 async def on_ready(): class MyView(discord.ui.View): @discord.ui.button(label="error", style=discord.ButtonStyle.red, emoji=None) async def menu1(self, interaction, button): await interaction.response.edit_message(content ="This is editited message") @discord.ui.button(label="Number1!", style=discord.ButtonStyle.primary, emoji="😎") async def menu2(self, interaction, button): await interaction.response.send_message("You clicked the button!") @discord.ui.button(label="Click me!", style=discord.ButtonStyle.green, emoji="🏓") async def menu3(self, interaction, button): embed = discord.Embed(title="This is my embed", description="Its a very cool embed", colour=discord.Colour.random(), timestamp= None) embed.set_author(name=f"SK-II") embed.set_thumbnail(url="https://media.discordapp.net/attachments/678292614398869513/1060893685916643380/image.png?width=598&height=669") embed.set_image(url="https://media.discordapp.net/attachments/678292614398869513/1060893685916643380/image.png?width=598&height=669") embed.add_field(name="This is 1 field", value="This field is just a value",inline=False) embed.add_field(name="This is 2 field", value="This field is just a value",inline=True) embed.add_field(name="This is 3 field", value="This field is just a value",inline=False) embed.set_footer(text=f"SKII here",icon_url="https://media.discordapp.net/attachments/678292614398869513/1060893685916643380/image.png?width=598&height=669") await interaction.response.edit_message(embed=embed) @bot.command() # Create a slash command async def menu(ctx): await ctx.reply("This is the menu!", view=MyView()) ```