discord bot 发送私信实现消息提醒
通过用户的discord创建用户的私信通道,bot机器人向用户的discord发过期提醒
在这里插入代import discord
from discord.ext import commands
# import yaml
import random
from datetime import datetime as dt
from discord.ext import tasks as discordTasks
from redis_util import RedisUtil
rs = RedisUtil()
TOKEN = "你bot的token"
client = discord.Client()
# set command prefix
client = commands.Bot(command_prefix='dmb ')
import json
async def overdue_user():
"""
过期用户
:return:
"""
embed = discord.Embed(
title='标题',
color=0xF1C40F,
url='链接'
)
embed.description='''
亲,您的账号即将到期,请及时续费,到期后将无法登陆,且不会统计数据,由此造成的数据丢失,淘账通概不负责。
Your account number of taozhangtong is about to expire. Please renew it in time. After the expiration,
you will not be able to log in, and there will be no statistical data. Taozhangtong will not be responsible for the loss of data
'''
data = rs.get_str_dict('overdue_user_discord') # 从redis里获取过期账户的discord用户id
print(data)
if data:
result = eval(data)
for i in result:
user = await client.fetch_user(int(i['discord_id'])) # 通过用户id获取user对象
await user.create_dm() # 创建私信通道
await user.dm_channel.send(embed=embed) # 向用户发送私信
rs.dele_all_redis('overdue_user_discord') # 删除缓存
# loop for sending message
@discordTasks.loop(seconds=10)
async def messageDaily():
await overdue_user()
# TODO: save to config file
# TODO: better help function
messageDaily.start()
client.run(TOKEN)