本篇教学会带大家部署一个自动参加Discord 抽奖机器人,会使用云端主机Google Cloud Function,好处是不会面临电脑关机时机器人就休息的情况,然后Google 也有提供一定使用额度内免费,所以基本上是完全零成本免费部署,让大家都可以解放双手和注意力,不会再错过任何Discord 的抽奖项目!
会有这篇文章的诞生,主要感谢Fomo dog 社群中的meta Simon 和saibalmars 狗友们,不断地改进和修正程式码(如下连结),才会有今天的Discord 抽奖机器人(云端稳定版本)
首先我们要申请Google Cloud Platform 的帐号,可以从这里进入,Google 目前有提供3 个月内300 美金的额度使用,而我们这次会使用GCP 里面的Google Cloud Function,以这次部署的程式的每用使用量来说,每月的花费在0 块台币以内。
接下来建立第一个专案,专案名称可以依自己喜好命名
接下来在搜寻栏中搜寻“Cloud Function”,并且选择建立函式
创建Cloud Function – Step 1
创建Cloud Function – Step 2
创建Cloud Function – Step 3
步骤3 里面的完整程式码如下: (请在bots_setting 中加入自己想要追踪的抽奖频道,关于参数设定可以参考Simon 的这篇)
import requests
import json
import random
import time
from datetime import datetime
def getlist(event, context):
# You can add more than one bot if you have multiple discord accounts.
bots_setting = [
{
"name": "bot1",
"authorizations": [
"xxxxxxx.xxxxxxx",
], # Please remember to put in your discord message authorization code
# spearate by comma if you have more than one token.
"channel_lists": [
{
"name": "Alpha Shark", # Nickname for the channel
"settings": {
"channel_id": "927557825486536764", # Channel's discord ID
"lottery_keyword": "React with \\ud83c\\udf89 to enter!", # Lottery keyword to detect: "React with 🎉 to enter!"
"emojis_to_click": ["%F0%9F%8E%89"] # emoji to click
}
}
]
}
]
limit=10
for bot in bots_setting:
for auth in bot["authorizations"]:
header = {
"Authorization": auth,
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36",
}
for channel in bot["channel_lists"]:
url = "https://discord.com/api/v9/channels/{}/messages?limit={}".format(channel["settings"]["channel_id"], limit)
try:
res = requests.get(url=url, headers=header)
messages_json = json.loads(res.text)
for message in messages_json:
try:
if channel["settings"]["lottery_keyword"] in json.dumps(message):
is_lottery_post = True
else:
is_lottery_post = False
if is_lottery_post:
print("-------------Lottery captured!--------------")
print("Date:", datetime.now())
print("Channel:", channel["name"])
print("Message ID:", message["id"])
print("Content:", message["content"])
print("embeds:", message["embeds"])
for emoji in channel["settings"]["emojis_to_click"]:
url = "https://discord.com/api/v9/channels/{}/messages/{}/reactions/{}/%40me".format(
channel["settings"]["channel_id"], message["id"], emoji)
requests.put(url=url, headers=header)
time.sleep(1)
except:
print("Failed to expand messages.")
pass
except:
print("Failed to get discord message.")
pass
在步骤4 里面的requirements.txt 里面加入requests,完整程式码如下:
# Function dependencies, for example:
# package>=version
requests
最后点击部署!但程式目前还不会自动执行,我们还要加上触发时间
最后选择建立!就完成触发条件设定啰
如何确定程式有执行?基本上只要没事去看一下Discord 抽奖频道,确认有没有点击抽奖就可以了
担心的话可以到Cloud Function > 纪录里面,可以看到程式执行过程(如下图)
那[零基础上手] Discord 自动参加抽奖机器人(云端稳定版) 就到这边感谢收看,如文章内容有误请不吝指正!