0
votes

I am trying to make a Discord BOT using Python. I am creating different kind of functions and now I want to create a function that lets the BOT to send a message in the general channel when someone joins the server

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)

I expect the output to be something like "Username has joined our server! Welcome."

1
Please be more specific and provide code snippets of the work done on your end for problem scoping. - Stoner
I edited your post; Do not show your bot's token to anyone. Someone who have your bot's token can have access to your entire bot. - Kaynn
I know, it's a different token - Lorenzo Orlando

1 Answers

0
votes

Assuming you are using version 1.3.0a... (or somewhere close)

You can basically follow the template that they have given on here. There is one for on_ready() and another for on_message(message).
Just find the respective event that you need in the docs, what you probably want is on_member_join(member),
like so:

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