为不和谐制作问候机器人

making a greetings bot for discord

所以,我正在尝试做一个机器人,当人们加入时从列表中获取问候语,然后随机化并提及他们,但我无法在选择用户的同时提及用户随机问候,我该怎么办?

import discord
import os
import random

client = discord.Client()

invites = ["has been invited by"]

username = client.get_user(id)

starter_greetings = [

  "hola como estas"
]

@client.event
async def on_ready():
  print('we have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
  if message.author == client.user:
    return

  msg = message.content

  channel = client.get_channel(940394154972823593)

  if any(word in msg for word in invites):
    await channel.send(username + random.choice(starter_greetings))

client.run(os.getenv('TOKEN'))

首先,您可以检查用户是否通过 on_member_join event, which runs whenever a user joins the server. You can send a message to a specific channel, and have your bot mention the user by using discord.Member.mention 加入服务器,而不是检查消息的内容。请看下面的示例。

starter_greetings = [
  "hola como estas",
  "welcome to the server",
  "enjoy your stay"
]

@client.event
async def on_member_join(member):
    join_channel = member.guild.get_channel(YOUR_CHANNEL_ID) # get the channel of your choice
    await join_channel.send(f"{member.mention} {random.choice(starter_greetings)}")
    # send the member and a random option from starter_greetings list