如何在每个公会中收听来自特定频道的消息

时间:2021-05-18 14:07:43

标签: python discord.py

我希望我的机器人收听我目前正在使用的名为“disminer-2”的每个频道

import discord
from discord.ext import commands
import os 
import asyncio
#from keepalive import keep_alive
from replit import db

client = commands.Bot(command_prefix = ">")
client.remove_command("help")

filtered = ["fuck","shit"]

@client.event
async def on_ready():
  print("We have logged in as {0.user}".format(client))
  print("User: user Id : Message Content")

@client.event
async def on_message(msg):
  
  chn_id = msg.channel.id
  
  #all the filter things
  
  if msg.channel.name != "disminer-1" or msg.author.id == 844182812977659904 or msg.author.bot == True:
    return
  if msg.content.lower() in filtered:
    await msg.reply("Your message contains a filtered word.")
    return
  if msg.content.lower() in "https://" or msg.content.lower() in "https://":
    await msg.reply("Links cannot be sent through, so don't try to advertize, idiot. And now your punishment is get pinged 10 times, loser!")
    for i in range (0,10):
      await asyncio.sleep(0.5)
      await msg.channel.send(f"{msg.author.mention}")
    return
  if  "discord.gg" in msg.content.lower() or "discord.com" in msg.content.lower():
    await msg.reply("Links cannot be sent through, so don't try to advert your discord server, idiot. And now your punishment is get pinged 10 times, loser!")
    for i in range (0,10):
      await asyncio.sleep(0.5)
      await msg.channel.send(f"{msg.author.mention}")
    return
  
  channel = discord.utils.get([x for x in client.get_all_channels() if x.type is discord.TextChannel], name="disminer-2")

  await channel.send(f"**{msg.author}:** {msg.content}")
  print(f"{msg.author} : ({msg.author.id}) : {msg.content}")
  await client.process_commands(msg)

@client.command()
async def test(ctx):
  for guild in client.guilds:
    for channel in guild.channels:
        await ctx.send (channel)


"""
Notes:
we might be able to do the for guild in thing
it seems to get all the channels.
"""

#keep_alive()
client.run(os.environ['BOTTOKEN'])

并且出现错误

<块引用>

忽略 on_message 中的异常 回溯(最近一次调用最后一次): 文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py”, 第 343 行,在 _run_event 中 等待 coro(args, kwargs) 文件“main.py”,第 45 行,在 on_message 中 等待 channel.send(f"{msg.author}:* {msg.content}") AttributeError: 'NoneType' 对象没有属性 'send'

感谢任何帮助。

我基本上是在尝试制作一个全局机器人,它可以读取 disminer-1 中的消息并将其发送到它所在的每个公会,并使用 disminer-2 频道。

1 个答案:

答案 0 :(得分:0)

您的代码有几个问题。 首先,您检查的频道类型有误。

discord.ChannelType.text

不是

discord.TextChannel

发现here

接下来,您的实现不会发送到每个频道。您将需要更改代码的执行方式。

for channel in client.get_all_channels():
    if (channel.type == discord.ChannelType.text) and (channel.name == "disminer-2"):
        await channel.send(f"**{msg.author}:** {msg.content}")