Trying to create a Discord Welcome/Leave Bot

2019-08-17 13:07发布

问题:

I'm trying to create a bot that publicly and privately (via DM) welcomes a new user to a Discord server AND sends a message to the Moderator channel when a user leaves the server.

I can get the welcome dm and welcome message working, but when I add the code after #Mod Leave Announcement nothing happens.

import all necessary commands and libraries
import discord
import asyncio
import logging

@client.event
async def on_ready():
print('logged in as')
print(client.user.name)
print(client.user.id)
print('-----')
newUserDMMessage = "Welcome DM"

#Public Welcome
@client.event
async def on_member_join(member):
print("Recognised that a member called " + member.name + " joined")
await client.send_message(member, newUserDMMessage)
await client.send_message(discord.Object(id='CHANNELID'), 'Welcome!')
print("Sent message to " + member.name)
print("Sent message about " + member.name + " to #CHANNEL")

#Mod Leave Announcement
@client.event
async def on_member_remove(member):
print("Recognised that a member called " + member.name + " left")
await client.send_message(discord.Object(id='CHANNELID'), member.name + ' left')
print("Sent message to #CHANNEL")

client.run('Token')

回答1:

Here's the code that's working currently. Thanks to Patrick Haugh for all the tips.

# import all necessary commands and libraries
import discord
import asyncio
import logging

@client.event
async def on_ready():
    print('logged in as')
    print(client.user.name)
    print(client.user.id)
    print('-----')

newUserDMMessage = "WELCOME!"

#Public Welcome
@client.event
async def on_member_join(member):
    print("Recognized that " + member.name + " joined")
    await client.send_message(member, newUserDMMessage)
    await client.send_message(discord.Object(id='CHANNELID'), 'Welcome!')
    print("Sent message to " + member.name)
    print("Sent message about " + member.name + " to #CHANNEL")

#Mod Leave Announcement
@client.event
async def on_member_remove(member):
    print("Recognized that " + member.name + " left")
    await client.send_message(discord.Object(id='CHANNELID'), '**' + member.mention + '** just left.')
    print("Sent message to #CHANNEL")

client.run('token')