iT邦幫忙

2023 iThome 鐵人賽

DAY 22
0

今天來說說,一些之後會應用到的基本語法
首先是將必定會使用到的

import discord
from discord.ext import commands #command裝飾器會使用到


intents = discord.Intents.all()
intents.members = True
bot = commands.Bot(command_prefix='/', intents=intents)#這個指的是觸發指令的特殊符號

#事件一,當機器人準備好的時候在後台顯示bot is ready
@bot.event
async def on_ready():
    print('bot is ready')
    
    
bot.run('token') #Day21說要產生的那個token

1. ctx觸發

這邊做一個小示範,比如說我需要看到目前機器人的延遲狀況

@bot.command()
async def ping(ctx):
    await ctx.send(f'{round(bot.latency*1000)}ms')

https://ithelp.ithome.com.tw/upload/images/20231003/20161505qMDfWbVreY.png

2. 圖片發送

再來是之後我們需要將圖片讓bot自動發送

@bot.command()
async def picture(ctx):
    pic=discord.File('C:\\Users\\OUOQUQ\\images\\明君旅棧\\住宿相片集4.jpg')
    await ctx.send(file=pic)

透過discord.File找到該圖片的路徑,然後就可以用send將他傳上去了
https://ithelp.ithome.com.tw/upload/images/20231003/20161505h8MGxKFFhE.png

注意!每一個function都要放在bot.run()之上喔

明天我們來用一個不用一直重開main.py的方法吧~


上一篇
[Day 21]將機器人加入到伺服器中
下一篇
[Day 23]Cog是個好東西
系列文
30天來打造一個方便的訂房機器人30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
arbin
iT邦新手 4 級 ‧ 2023-10-03 16:54:35

中秋快樂

我要留言

立即登入留言