提问者:小点点

不一致pypython—我想创建一个函数,当用户加入服务器时,该函数在服务器中发送一条全局消息


我正在尝试使用Python创建一个Discord 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)

我希望输出类似“Username已加入我们的服务器!欢迎”


共1个答案

匿名用户

假设您使用的是版本1.3。0a。。。(或在附近)

你基本上可以遵循他们在这里给出的模板。有一个用于on_ready(),另一个用于on_message(消息)

# ...
@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!")
# ...