我正在尝试为不和谐服务器创建一个机器人,该机器人仅侦听特定消息,然后将其删除,然后将用户引向其他文本通道(在可点击链接中提及)

这是我现在所拥有的:

import Discord
import asyncio


client = discord.Client()


@client.event
async def on_message(message):
    msg = '{0.author.mention}\nWrong text channel\nUse '.format(message)
    if message.content.startswith('!p'):
        await client.delete_message(message)
        await client.send_message(message.channel, msg)
    return


client.run('')


理想情况下,我也想使用startswith()而不是('!p')搜索整个列表,也要忽略来自特定文本通道的所有消息,但我也不知道该怎么做

最佳答案

当然,只需添加text_channel = client.get_channel('1234567890')并用text_channel.mention引用其提及(其中1234567890是您要链接的频道的ID)

所以代码最终看起来像这样

@client.event
async def on_message(message):
  text_channel = client.get_channel('1234567890')
  msg = '{0.author.mention}\nWrong text channel\nUse {1.mention}'.format(message,text_channel)
  if message.content.startswith('!p'):
      await client.delete_message(message)
      await client.send_message(message.channel, msg)
  return


关于第二个问题,您可以执行以下操作

  arr = ['!p','!a','!b']
  for a in arr:
    if message.content.startswith(a):
      break
  else:
    return


并完全删除if message.content.startswith('!p'):

要忽略特定通道,只需在函数顶部执行if message.channel.id == "9876543210":9876543210是要忽略其命令的通道的ID)
经过这些更改,代码看起来像这样

@client.event
async def on_message(message):
  if message.channel.id == "9876543210":
    return
  arr = ['!p','!a','!b']
  for a in arr:
    if message.content.startswith(a):
      break
  else:
    return
  text_channel = client.get_channel('1234567890')
  msg = '{0.author.mention}\nWrong text channel\nUse {1.mention}'.format(message,text_channel)
  await client.delete_message(message)
  await client.send_message(message.channel, msg)
  return

关于python - Discord.py-> channel.mention,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52132913/

10-12 21:42