自定义 discord bot - 如何避免超过 Discord API 速率限制?
Custom discord bot - How to avoid exceeding Discord API rate limit?
我在语言学习服务器上托管了一个机器人。目前bot有两个功能,分别是:
- 扫描#welcome 频道中的消息并删除所有包含任何链接的消息
- 当成员获得我们拥有的五种流畅角色(初级、中级、高级、流利和母语)中的一种时,删除他们的访客角色,并在他们的流畅角色时将访客角色恢复。流畅度角色通过Dyno角色按钮功能分配。
我设法将代码拼接在一起 运行 这两个函数。我得到消息扫描 运行ning 没问题(已经 运行 过夜了,没问题)。今天早些时候,我让访问者自动角色删除功能开始工作。虽然几个小时后我收到了 Discord 的限速。
我不是狂热的程序员。我认为我的代码中有很多低效之处可能导致了这种情况。如果你们能指出我做错了什么,或者是否有办法避免触发速率限制,我将不胜感激。提前致谢!
P.S.: 代码是 运行ning 并托管在 repl.it 中,并带有 keep-alive 组合。这可能是问题的一部分吗?我 运行 正在使用的功能是否需要专门的托管服务?
这是我使用的代码:
import discord
import os
import datetime
from keep_alive import keep_alive
intents = discord.Intents().all()
client = discord.Client(intents=intents)
#ct = datetime.datetime.now(tz=pytz.timezone('Asia/Hong_Kong'))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello')
###########link remover###########
channelID = 840910671084xxxxxx
channelname = 'welcome'
msg_content = message.content.lower()
channelmsg = client.get_channel
links = ['http', 'https','.com']
#channel lock
if channelname == str(message.channel):
exclusion = [936293188149xxxxxx, 934739311180xxxxxx, 943017611439xxxxxx]
#link detection
if any(word in msg_content for word in links):
#exclude admins
if any(rolex in [y.id for y in message.author.roles] for rolex in exclusion):
print('{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()), "Links detected by Admin/Staff. Ignored.")
return
else:
print('{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()), "Links detected by members. Deleted.")
await message.delete()
channel = client.get_channel(channelID)
await channel.send("Sorry! To prevent spam & scam messages, no links are allowed in <#840910671084xxxxxx>.")
#fluency role pick
@client.event
async def on_member_update(before, after):
roles_id = [932635921516xxxxxx, 932636823933xxxxxx, 932636866103xxxxxx, 932636911468xxxxxx, 932637000194xxxxxx, 935172897092xxxxxx]
role_visitor = discord.utils.get(after.guild.roles, id=939440361896xxxxxx)
if any(roles in [y.id for y in after.roles] for roles in roles_id):
if after.roles is not None:
return
elif(939440361896xxxxxx not in [y.id for y in after.roles]):
return
print('{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()), "Changes to role happened. Visitor role removed.")
await after.remove_roles(role_visitor)
else:
if (939440361896xxxxxx in [y.id for y in after.roles]):
return
print('{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()), "Changes to role happened. Visitor role added.")
await after.add_roles(role_visitor)
keep_alive()
client.run(os.environ['TOKEN'])
当我的机器人总是因为超过速率限制而被 discord 的 api 暂时禁止时,我总是很沮丧,这要归功于我的朋友一遍又一遍地发送垃圾命令。但是没有办法阻止 replit 上的问题。但它可以修复。转到 shell 并键入 kill 1。然后重新启动你的机器人。一切都将被重置,你会很高兴去。顺便代码没问题
我在语言学习服务器上托管了一个机器人。目前bot有两个功能,分别是:
- 扫描#welcome 频道中的消息并删除所有包含任何链接的消息
- 当成员获得我们拥有的五种流畅角色(初级、中级、高级、流利和母语)中的一种时,删除他们的访客角色,并在他们的流畅角色时将访客角色恢复。流畅度角色通过Dyno角色按钮功能分配。
我设法将代码拼接在一起 运行 这两个函数。我得到消息扫描 运行ning 没问题(已经 运行 过夜了,没问题)。今天早些时候,我让访问者自动角色删除功能开始工作。虽然几个小时后我收到了 Discord 的限速。
我不是狂热的程序员。我认为我的代码中有很多低效之处可能导致了这种情况。如果你们能指出我做错了什么,或者是否有办法避免触发速率限制,我将不胜感激。提前致谢!
P.S.: 代码是 运行ning 并托管在 repl.it 中,并带有 keep-alive 组合。这可能是问题的一部分吗?我 运行 正在使用的功能是否需要专门的托管服务?
这是我使用的代码:
import discord
import os
import datetime
from keep_alive import keep_alive
intents = discord.Intents().all()
client = discord.Client(intents=intents)
#ct = datetime.datetime.now(tz=pytz.timezone('Asia/Hong_Kong'))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello')
###########link remover###########
channelID = 840910671084xxxxxx
channelname = 'welcome'
msg_content = message.content.lower()
channelmsg = client.get_channel
links = ['http', 'https','.com']
#channel lock
if channelname == str(message.channel):
exclusion = [936293188149xxxxxx, 934739311180xxxxxx, 943017611439xxxxxx]
#link detection
if any(word in msg_content for word in links):
#exclude admins
if any(rolex in [y.id for y in message.author.roles] for rolex in exclusion):
print('{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()), "Links detected by Admin/Staff. Ignored.")
return
else:
print('{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()), "Links detected by members. Deleted.")
await message.delete()
channel = client.get_channel(channelID)
await channel.send("Sorry! To prevent spam & scam messages, no links are allowed in <#840910671084xxxxxx>.")
#fluency role pick
@client.event
async def on_member_update(before, after):
roles_id = [932635921516xxxxxx, 932636823933xxxxxx, 932636866103xxxxxx, 932636911468xxxxxx, 932637000194xxxxxx, 935172897092xxxxxx]
role_visitor = discord.utils.get(after.guild.roles, id=939440361896xxxxxx)
if any(roles in [y.id for y in after.roles] for roles in roles_id):
if after.roles is not None:
return
elif(939440361896xxxxxx not in [y.id for y in after.roles]):
return
print('{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()), "Changes to role happened. Visitor role removed.")
await after.remove_roles(role_visitor)
else:
if (939440361896xxxxxx in [y.id for y in after.roles]):
return
print('{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()), "Changes to role happened. Visitor role added.")
await after.add_roles(role_visitor)
keep_alive()
client.run(os.environ['TOKEN'])
当我的机器人总是因为超过速率限制而被 discord 的 api 暂时禁止时,我总是很沮丧,这要归功于我的朋友一遍又一遍地发送垃圾命令。但是没有办法阻止 replit 上的问题。但它可以修复。转到 shell 并键入 kill 1。然后重新启动你的机器人。一切都将被重置,你会很高兴去。顺便代码没问题