当前位置: 首页 > 知识库问答 >
问题:

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

伯彦君
2023-03-14

我希望我的机器人能够收听我当前使用的每个名为“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_消息回溯中的异常(最近一次调用):文件“/opt/virtualenvs/python3/lib/python3.8/site packages/discord/client.py”,第343行,in_run_事件等待coro(args,kwargs)文件“main.py”,第45行,in_消息等待通道。send(f“{msg.author}:{msg.content}”)AttributeError:'NoneType'对象没有属性'send'

任何帮助感谢。

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

共有1个答案

郝君博
2023-03-14

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

它的

discord.ChannelType.text

discord.TextChannel

在这里找到的。

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

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}")
 类似资料:
  • 下午好 我再次有一个关于不和谐蟒蛇机器人的问题。所以我想做一些支持脚本之类的东西。我的想法是给所有的支持者写一个命令,如果他们值班的话,保存在一个json文件中。这没问题,但我的问题是,我希望用户在连接到一个名为“支持室”的特定语音频道时,收到多少支持者在值班。有人知道我如何写一个脚本,当一个成员加入这个特定的语音频道时,他会收到一条信息,例如:member.send(“你现在在支持室”。请等到支

  • 问题内容: 要开始执行两个goroutine的无限循环,我可以使用以下代码: 收到味精后,它将启动一个新的goroutine并永远继续下去。 我现在想对N个goroutine具有相同的行为,但是在这种情况下select语句将如何显示? 这是我开始使用的代码,但是我对如何编写select语句感到困惑 问题答案: 您可以使用reflect包中的函数执行此操作: Select执行案例列表中描述的选择操作

  • 如何删除文本频道中的每条消息?这起作用了,但速度很慢。(删除所有内容可能需要15秒,具体取决于数量)我无法使用该选项,因为速度在我的实现中很重要。 我还尝试删除整个通道并创建一个具有相同属性的新通道,这也可以工作,但当机器人此后试图向通道发送消息时出现了一些问题。(未知通道错误) 进一步说明:这必须能够删除多达数千条消息。 如果您对我的问题有任何疑问,请询问,我会尝试相应地编辑它。

  • 我在学习不和谐。py和我想让机器人发送嵌入的消息,就像。(@user已加入!)当用户加入特定的语音频道(如music.vc)时,仅在特定的文本频道加入(音乐频道) 像这样 当用户加入时 还 当用户离开时

  • 我在discord中制造了一个discord机器人。js和我正试图发出一个欢迎命令。我正在尝试向服务器中的特定通道发送消息。我不知道该怎么做,因为在discord中更新了。因为我以前做过。你能帮助我吗? 这是我的代码的一大块:

  • 我对javascript和discord.js很陌生,有人知道如何让机器人加入一个频道,播放一个文件然后离开吗? 以下是我尝试过的: 现在,它将发送消息加入语音通道,而不管我是否在其中,如果我在其中,它就不会加入语音通道并播放文件。任何帮助都很感激。