我在这里创建了这些代码,当有人有权“将@user 送到法庭”时,它会说“将@user 送到法庭”,但这里的代码不起作用:
import discord
from discord.ext import commands
import ctx
import re
class MyClient(discord.Client):
async def on_ready(self):
print('Logged on as', self.user)
async def on_message(self, message):
if message.author == self.user:
return
messageContent = message.content
if len(messageContent) > 0:
if re.search("^send.*court$", messageContent):
user_id = message.mentions[0].id
user = self.get_user(user_id)
await message.channel.send("sending", user, "to court!")
client = MyClient()
client.run('My secret token')
答案 0 :(得分:2)
您正在发送一个用户对象。要发送提及,您必须"<@{userid}"
提及
这是你需要做的
await message.channel.send(
f"sending <@{user_id}> to court!"
)
答案 1 :(得分:0)
首先你不能使用 await message.channel.send("sending", user, "to court!")
,因为你想在需要 1 时传递 3 个参数,你只需要格式化字符串。其次,您不必获取用户的 id,然后使用 get_user(id)
获取用户对象,因为您已经在 message.mentions[0]
中拥有此用户对象,这里是最终示例:
import discord
from discord.ext import commands
import re
class MyClient(discord.Client):
async def on_ready(self):
print("Logged on as", self.user)
async def on_message(self, message):
if message.author == self.user:
return
messageContent = message.content
if len(messageContent) > 0:
if re.search("^send.*court$", messageContent):
await message.channel.send(
f"sending {message.mentions[0].name} to court!"
)
client = MyClient()
client.run("secret token")