当使用on_member_join时,如何从bot获取要发送到所需特定频道的消息?

2024-04-25 06:56:16 发布

您现在位置:Python中文网/ 问答频道 /正文

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

到目前为止,我的代码是:

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)


Tags: tonameimportclientevent消息readasync
2条回答

我会这样做的

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)

您可以使用disocrd.utils.get通过其id获取#欢迎频道。此外,无需使用(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

相关问题 更多 >