DISCORD.PY PYTHON-我要创建一个函数,当用户加入服务器时在服务器中发送全局消息

时间:2019-09-08 12:36:19

标签: python discord discord.py pypi

我正在尝试使用Python制作Discord BOT。我正在创建其他类型的函数,现在我想创建一个函数,当有人加入服务器时,该函数可以让BOT在常规通道中发送消息

import discord
import random

from discord.ext import commands

token = 'BOT_TOKEN'
client = commands.Bot(command_prefix = '!')

@client.event
async def on_ready():
    print(f'{client.user.name} is now ready')

@client.command(aliases = ['ask', 'chiedi'])
async def _ask(ctx, *, question):
    file_object = open("quotes.txt")
    quotes = file_object.readlines()
    file_object.close()

    rnd = random.choice(quotes)
    await ctx.send(rnd)

@client.event
async def on_message(message):
    author = message.author
    content = message.content
    log = '{}: {}'.format(author, content)
    with open("logs.txt", "a+") as my_file:
        my_file.write(log + "\n")
        my_file.close()

'''
func print_msg
    once the user joins the server
    print message to greet him

'''

client.run(token)

我希望输出类似于“用户名已加入我们的服务器!欢迎使用。”

1 个答案:

答案 0 :(得分:0)

假设您使用的是1.3.0a版...(或附近版本)

您基本上可以遵循他们在here上提供的模板。 on_ready()有一个,on_message(message)有一个。
只需在文档中找到您需要的相应事件,您可能想要的是on_member_join(member)
就像这样:

# ...
@client.event
async def on_member_join(member):
    await member.guild.text_channels[0].send("A message sent to the first channel because a member joined!")
# ...