首先要來到DISCORD developers
登入已經註冊的帳號

點選左邊的Applications後再點選右邊的New Application

輸入機器人名稱後點Creat初步機器人就創建完成了

General Information主要就是設定機器人的基本資料和查看ID
image
要記住OAuth2紅色框中的號碼,PYTHON程式中會使用到
先來到Bot點選Add Bot
成功後就會有提示A wild bot has appeared!
 
接著回到OAuth2點選SCOPES中的bot之後下面會顯示出一條網址,是用來給我們邀請機器人加入已經創建好的DISCORD伺服器

image

複製網址到瀏覽器上就可以直接邀請機器人加入伺服器

套件的安裝方式

python -m pip install -U discord.py

或是其他方式安裝套件discord.py

安裝完畢後撰寫PYTHON程式碼讓機器人可以上線登入,之後要維持機器人的運營都要開著程式即可

 
機器人指令PYTHON程式
 
讓機器人回話,針對特定語句下面範例是針對"!hello"語句進行回話,回覆內容如果是YOUTUBE影片網址則可以直接貼出影片
import discord
async def log_reaction():
await ctx.channel.send(f"{ctx.user} reacted with {ctx.emoji}")
client = discord.Client()
async def on_ready():
print(f'目前登入身份:{client.user}')
game = discord.Game('321木頭人')
await client.change_presence(status=discord.Status.idle, activity=game)
li = ['AA','BB']
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == '!hello':
await message.channel.send('你好呀我是Banana man')
client.run('填入TOKEN碼')
 
讓機器人偵測特定語句,且刪除對話
import discord
async def log_reaction():
await ctx.channel.send(f"{ctx.user} reacted with {ctx.emoji}")
client = discord.Client()
async def on_ready():
print(f'目前登入身份:{client.user}')
game = discord.Game('321木頭人')
await client.change_presence(status=discord.Status.idle, activity=game)
li = ['AA','BB']
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content in li:
await message.delete()
await message.channel.send('偵測到違禁字串,啟動刪除')
client.run('填入TOKEN碼')
 
重複我們說過的話
import discord
async def log_reaction():
await ctx.channel.send(f"{ctx.user} reacted with {ctx.emoji}")
client = discord.Client()
async def on_ready():
print(f'目前登入身份:{client.user}')
game = discord.Game('321木頭人')
await client.change_presence(status=discord.Status.idle, activity=game)
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!replay'):
tmp = message.content.split(" ", 2)
if len(tmp) == 1:
await message.channel.send("輸入錯誤格式指令")
else:
await message.channel.send(tmp[1])
client.run('輸入TOKEN碼')

 

偵測有人進入指定語音聊天室

import discord
client = discord.Client()
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == '!hello':
await message.channel.send('你好呀我是Banana man')
@client.event
async def on_voice_state_update(member, before, after):
if before.channel is None and after.channel is not None:
channel = client.get_channel(頻道ID)
await channel.send(f'hello{member} 進入語音聊天室了!!!')
client.run('輸入TOKEN碼')

 

機器人回應帶著貼圖
import discord
client = discord.Client()
embed = discord.Embed()
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == '!圖片':
embed.set_thumbnail(url = f"圖片網址")
await message.channel.send(embed=embed)
client.run('TOKEN碼')

 

 

 

 

 

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
arrow
arrow
    創作者介紹
    創作者 Saioyan梟夜 的頭像
    Saioyan梟夜

    Saioyan梟夜

    Saioyan梟夜 發表在 痞客邦 留言(0) 人氣()