Discord.py : on_message() 在某些服务器上不起作用

时间:2021-03-10 20:22:15

标签: discord discord.py

我正在为客户编写一个脚本,如果有人在某些特定频道中要求编辑,它会通知您。

但是,on_message() 函数不会检测两台服务器(都是合作服务器)上的消息,但可以在任何其他服务器上检测。

(请注意,它也是作为 selfbot 运行的)。

代码如下:

import discord
import time
import json


with open('config.json', 'r') as f:

    json_f = json.load(f)

    TOKEN = json_f['Token']
    words = json_f['words']
    channels = json_f['channels']
    blacklist = json_f['blacklisted']
    export_channel = json_f['export_channel']
    blackl_users = json_f['blacklisted_users']


client = discord.Client()


@client.event
async def on_ready():
    print('Ready !\nLogged as :', client.user.name)



@client.event
async def on_message(message):

    send_in = client.get_channel(export_channel)


    if message.author.id in blackl_users:
        return

    for i in blacklist:
        if i in message.content.lower():
            return

    if (message.channel.id in channels)==False:
        return


    for i in words:
        if i in message.content.lower():

            c_time = time.localtime(time.time())
            m_time = str(c_time.tm_hour) + ':' + str(c_time.tm_min) + ' the ' + str(c_time.tm_mon) + '/' + str(c_time.tm_mday) + '/' + str(c_time.tm_year)

            title_content = 'Message found in the server "' + str(message.guild) + '" :'

            desc_content = '**Channel name :** #' + message.channel.name + '\n**Channel ID : **' + str(message.channel.id) + '\n\n**By : **' + str(message.author) + '\n**Author ID :** ' + str(message.author.id) + '\n\n**At : **' + m_time + '\n\n**Message ID : **' + str(message.id) + '\n**Message content :**\n' + message.content


            embed = discord.Embed(title=title_content, description=desc_content, color=0x7d34eb)

            await send_in.send(embed=embed)

            return



client.run(TOKEN, bot=False)

1 个答案:

答案 0 :(得分:0)

我不推荐使用 discord.py 来编写 selfbots(如果您真的想使用 discord.py,请阅读此答案的最后一句)。 Discord.py 是为机器人帐户维护的,通过使用它,您可能会冒着运行客户端通常不会做的事情的风险(因此,可被不和谐标记)。 无论如何,用户帐户需要首先订阅一个公会才能收听来自该公会的消息(这是一个 op14,termed "LazyRequest" by Luna)。 我不确定如何使用 discord.py 执行此操作,但我知道如何使用名为 discum 的不同包装器执行此操作。方法如下:

import discum
bot = discum.Client(token='token here')

@bot.gateway.command
def somefunctionname(resp):
    if resp.event.ready_supplemental:
        bot.gateway.request.lazyGuild(FIRSTGUILDID, {FIRSTCHANNELID: [[0,99]]}, typing=True, threads=False, activities=True, members=[])
        bot.gateway.request.lazyGuild(SECONDGUILDID, {SECONDCHANNELID: [[0,99]]}, typing=True, threads=False, activities=True, members=[])
    if resp.event.message:
        print(resp.raw) #print the raw response from discord

bot.gateway.run()

请注意,discum 仅针对用户帐户进行编写和维护。

刚刚发现这个......如果你想使用discord.py,这是另一个解决方案(与上面的代码相同): https://github.com/Rapptz/discord.py/issues/6340#issuecomment-765868893