用python实现Discord自动聊天机器人,撸白名单必备!提供源码!

教程总览

  1. 下载安装python

  2. 下载安装Visual Studio Code

  3. 创建文件复制源码

  4. 配置参数和设置

  5. 启动程序,聊天聊天

详细步骤

1.下载安装python

python官网下载链接:https://www.python.org/downloads/

post image

点击下载 Python 按钮下载并安装

2.下载安装Visual Studio Code

VS code官方下载链接:https://code.visualstudio.com/Download

post image

根据自己的电脑点击下载对应的版本

3.创建文件复制源码

①打开vs code软件,创建文件discord_bot.py ,并把代码粘贴进去

post image

代码如下(可直接粘贴):

# -*- coding: utf-8 -*-


import requests
import json
import random
import time
import re

"""
获取剧本内容
随机返回一个剧本列表里的内容
"""
def gen_context():
    """自定义剧本,根据自己需求改context_list里的内容"""
    context_list = [
        "肝", "加油", "肝啊", "快升级了", "肝肝更健康", "只能肝",
        "拿白", "继续努力", "努力拿白", "..", 
    ]
    text = random.choice(context_list)
    return text


"""
获取频道聊天记录,
并随机返回一个
"""
def get_context():
    chanel_list = ['请输入你需要获取聊天记录的频道id']
    headr = {
        "Authorization": "请输入你自己的Authorization",
        "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"
    }
    chanel_id = random.choice(chanel_list)
    url = "https://discord.com/api/v9/channels/{}/messages?limit=100".format(
        chanel_id)
    res = requests.get(url=url, headers=headr)
    result = json.loads(res.content)
    result_list = []
    for context in result:
        if ('<') not in context['content']:
            if ('@') not in context['content']:
                if ('http') not in context['content']:
                    if ('?') not in context['content']:
                        result_list.append(context['content'])

    return random.choice(result_list)


def chat():
    chanel_list = ['请输入你要肝白名单的频道id']
    authorization_list = ['请输入你自己的Authorization']
    for authorization in authorization_list:
        header = {
            "Authorization": authorization,
            "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 chanel_id in chanel_list:
            """如果使用预设剧本,下面的content后就用gen_context(),如果需要随机频道聊天记录,则换成get_context()"""
            msg = {
                "content": gen_context(),
                "nonce": "82329451214{}33232234".format(random.randrange(0, 1000)),
                "tts": False
            }
            url = 'https://discord.com/api/v9/channels/{}/messages'.format(
                chanel_id)
            try:
                res = requests.post(url=url, headers=header,
                                    data=json.dumps(msg))
                print(res.content)
            except Exception:
                print(Exception)
            continue
        # 取10秒到20之间的一个随机数,作为循环的间隔时间。
        time.sleep(random.randrange(10, 20))


if __name__ == '__main__':
    while True:
        try:
            print('start')
            chat()
            # 取30秒到40之间的一个随机数,作为机器人发送消息的间隔时间。
            sleeptime = random.randrange(30, 40)
            time.sleep(sleeptime)
        except:
            pass
        continue

4.配置参数和设置剧本

(参数:频道id和你的授权id)

①获取频道id

打开google浏览器,并登录网页版discord

进入你想要肝白名单的聊天频道

浏览器地址 最后一个/ 后的一串数字就是你的频道id

post image

然后将代码下述位置改为你的频道id

post image

②获取自己的授权id

按F12进入开发者模式,并点击network,如下

post image

在频道中发送一句话

点击开发者工具左边的typing,然后在右侧的headers里找到你的授权id(authorization) 注意:这个id不要让其他人知道!

post image

然后将代码下述位置改成你的授权id

post image

③选择模式(固定剧本还是从其他频道里随机聊天记录)

如果是想要固定剧本,就在chat方法中,把msg的content设置为gen_context()

如果是想要从其他聊天频道中随机聊天记录,则设为get_context()

post image

用剧本的话,在下面gen_context()方法里设置你自己的剧本

post image

用其他聊天频道中随机聊天记录,就在get_context()方法中设置频道id和你自己的授权id

post image

5.启动程序,自动聊天

①给vs code安装python插件

如果第一次用vs code

需要安装在扩展商店安装python插件

post image

②安装requests

第一次用的话,需要安装requests

打开终端,输入pip3 install requests,然后回车安装就行了

post image

③点击右上角的运行按钮,就会自动聊天啦

post image
post image

如果想要关闭,就在vs code里直接终止终端就行了

ps:多账号多频道一起肝也可以

只需增加chat方法中的频道列表和授权列表就行了~

post image

以上就是教程的全部了,后续也会出相关的视频教程,敬请期待。

欢迎添加我的微信,一起交流学习

post image