使用on_member_join时,如何从漫游器获取消息以发送到所需的特定通道?

时间:2020-08-16 05:29:52

标签: python discord bots discord.py discord.py-rewrite

我正在尝试创建一个在成员加入公会时发送欢迎消息的机器人,但是我不知道如何将消息发送到我想要的特定频道。

到目前为止,我的代码:

import discord
from discord.ext import commands
import random

client = commands.Bot(command_prefix='.', case_insensitive=True)


@client.event
async def on_ready():
    await client.change_presence(status=discord.Status.online, activity=discord.Game(': The Moon Song'))
    print("bot is read to use")

@client.event
async def on_member_join(member: discord.Member = None):
    name = member.display_name
    embed = discord.Embed(title=(f'{member} has joined the guild! \nWelcome!'), description = 'please nativate to #rules and read all relavent information\nWe hope you enjoy your stay here <3', colour= 0xFFF0A1,)  
    embed.set_image(url='https://media.giphy.com/media/26xBz5092fHa8usx2/giphy.gif')
    await member.send(embed=embed)

2 个答案:

答案 0 :(得分:0)

您可以使用disocrd.utils.get通过其ID获取#welcome频道。另外,无需使用(member: discord.Member = None):,这是使用disocrd.utils.get-

的方法
import discord
from discord.ext import commands
import random

client = commands.Bot(command_prefix='.', case_insensitive=True)


@client.event
async def on_ready():
    await client.change_presence(status=discord.Status.online, activity=discord.Game(': The Moon Song'))
    print("bot is read to use")

@client.event
async def on_member_join(member):
    name = member.display_name
    embed = discord.Embed(title=(f'{name} has joined the guild!\nWelcome!'), description = 'please nativate to #rules and read all relavent information\nWe hope you enjoy your stay here <3', colour= 0xFFF0A1,)
    embed.set_image(url='https://media.giphy.com/media/26xBz5092fHa8usx2/giphy.gif')
    await member.send(embed=embed)
    channel = discord.utils.get(member.guild.text_channels, id="<your #welcome channel's ID>")
    await channel.send(embed=embed)

参考-

discord.on_message

discord.utils.get

答案 1 :(得分:0)

这就是我要做的

import discord
from discord.ext import commands
import random

client = commands.Bot(command_prefix='.', case_insensitive=True)


@client.event
async def on_ready():
    await client.change_presence(status=discord.Status.online, activity=discord.Game(': The Moon Song'))
    print("bot is read to use")

@client.event
async def on_member_join(member: discord.Member = None):
    channel = client.get_channel(ID) # Where ID is your welcome channel's ID.
    name = member.display_name
    embed = discord.Embed(title=(f'{member} has joined the guild! \nWelcome!'), description = 'please nativate to #rules and read all relavent information\nWe hope you enjoy your stay here <3', colour= 0xFFF0A1,)  
    embed.set_image(url='https://media.giphy.com/media/26xBz5092fHa8usx2/giphy.gif')
    await channel.send(embed=embed)