46
社区成员




discord是web3社区常用的聊天工具。通过discord,用户不仅能方便地加入各种群组(服务器),也能轻松地创建属于自己的群组(服务器)。
而在discord生态中,discord机器人是非常重要的组成部分。它可以为管理员提供多种服务:用户鉴权、用户管理、广告消息清理、空投发放,也能为普通用户提供各类自助服务、发送欢迎消息等等。
而基于python的discord.py正是编写discord机器人最为常用的工具之一。本文会带领大家如何从零开始,创建属于自己的discord机器人。
首先,访问discord开发者后台,注册新的应用
https://discord.com/developers/applications
在新建应用的设置里,选择Bot
选择Add Bot进行机器人的创建
对新建的机器人点击Reset Token进行token的设置,设置完毕后务必记录下该token,以备后续使用
在设置中,选择OAuth2 -> URL Generator,勾选需要的权限。本文只需要用bot下面的发送消息和管理消息两个权限
设置完毕后,点击下方的Copy按钮,获取机器人的URL格式如下
https://discord.com/api/oauth2/authorize?client_id=<your_client_id>&permissions=10240&scope=bot
此时,在浏览器打开该URL,即可将新创建的机器人加入到你所管理的指定discord服务器中
注意,需要你授权相应权限给机器人
为python环境安装discord.py的依赖
pip install discord.py
下面开始编写程序
import discord
import asyncio
import re
# 这里填写创建机器人时生成的token
discord_bot_token = "xxxxxx"
# 如果希望通过代理连接discord服务器,可以设置http proxy参数
# client = discord.Client(proxy="http://127.0.0.1:10809")
client = discord.Client()
@client.event
async def on_ready():
print("Bot client logged-in as: %s" % client.user)
# 展示 `Playing xxx` 状态
custom_activity = discord.Game('discord.py')
await client.change_presence(status=discord.Status.online, activity=custom_activity)
@client.event
async def on_member_join(member):
# 发送结构化消息
embed_str = discord.Embed(title="my test server",
description=f"Hi {member.name}, "
f"welcome to the official {member.guild.name} Discord Server!",
color=0x00ff00)
await member.create_dm()
await member.dm_channel.send(embed=embed_str)
# 发送普通文本消息
# await member.dm_channel.send(
# f'Hi {member.name}, welcome to the official {member.guild.name} Discord Server!'
# )
@client.event
async def on_message(message):
# 排除bot自身的消息
if message.author == client.user:
return
# ping/pong测试,然后删除测试消息
if message.content == 'ping':
tmp_msg = await message.channel.send('pong')
await asyncio.sleep(3)
await tmp_msg.delete()
await message.delete()
# 返回at机器人的消息
elif client.user.mentioned_in(message):
trim_mentioned_msg = re.sub("<@!?(\d+)>", "", message.content).strip()
await message.channel.send(
f"{message.author.mention} Message received: {trim_mentioned_msg}")
else:
print("Do nothing with message: %s" % message.content)
# 启动机器人
client.run(discord_bot_token)
代码说明:
# 展示 `Playing ` 状态
custom_activity = custom_activity = discord.Game("a game")
# 展示 `Streaming ` 状态
custom_activity = discord.Streaming(name="a live channel", url="https://example.com/your_live_channel_url")
# 展示 `Listening ` 状态
custom_activity = discord.Activity(type=discord.ActivityType.listening, name="a song")
# 展示 `Watching ` 状态
custom_activity = discord.Activity(type=discord.ActivityType.watching, name="a movie")
# 展示 `Competing in ` 状态
custom_activity = discord.Activity(type=discord.ActivityType.competing, name="a game")
运行上面编写好的程序后,就能在对应discord服务器中看到机器人上线,并展示指定的状态
发送at机器人的文本消息
如果输入ping,机器人还会返回pong消息,并在3秒钟后删除ping/pong的消息。
至此,我们的第一个discord机器人就完成了!
请问国内报错Cannot connect to host discord.com:443 ssl:default [The semaphore timeout period has expired].
这个该怎么解决呀,我试过vpn都不行
Traceback (most recent call last):
File "C:\Users\64420\Desktop\My_Bot.py", line 10, in
client = discord.Client()
TypeError: Client.init() missing 1 required keyword-only argument: 'intents'
您好,出现了这个错误,请问怎么修改?