欢迎/再见使用discord.py

问题描述 投票:0回答:2

我正在尝试创建一个机器人来迎接加入服务器的用户。但是要让人们在服务器本身受到欢迎而不是作为DM(我发现的大多数教程都教你如何做)。

这是我到目前为止所提出的。

@bot.event
async def on_member_join(member):
    channel = bot.get_channel("channel id")
    await bot.send_message(channel,"welcome") 

但是,它不起作用,而是抛出这个错误。

Ignoring exception in on_member_join
Traceback (most recent call last):
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python36\lib\site- 
packages\discord\client.py", line 307, in _run_event
yield from getattr(self, event)(*args, **kwargs)
File "C:\Users\Lenovo\Documents\first bot\bot.py", line 26, in 
on_member_join
await bot.send_message(channel,"welcome")
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python36\lib\site- 
packages\discord\client.py", line 1145, in send_message
channel_id, guild_id = yield from self._resolve_destination(destination)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python36\lib\site- 
packages\discord\client.py", line 289, in _resolve_destination
raise InvalidArgument(fmt.format(destination))
discord.errors.InvalidArgument: Destination must be Channel, PrivateChannel, 
User, or Object. Received NoneType
python bots discord discord.py
2个回答
0
投票

你没有将正确的id传递给get_channel,所以它正在返回None。获得它的一种快速方法是调用命令

from discord.ext import commands

bot = commands.Bot(command_prefix='!')

@bot.command(pass_context=True)
async def get_id(ctx):
    await bot.say("Channel id: {}".format(ctx.message.channel.id))

bot.run("TOKEN")

您还可以修改命令,以便始终在Member加入的服务器上发布具有特定名称的频道

from discord.utils import get

@bot.event
async def on_member_join(member):
    channel = get(member.server.channels, name="general")
    await bot.send_message(channel,"welcome") 

0
投票

帕特里克·霍的答案可能是你最好的选择,但这里有一些要记住的事情。

Member对象包含公会(服务器)和服务器包含的文本通道。通过使用member.guild.text_channels,即使服务器没有“常规”聊天,您也可以确保频道存在。

@bot.event
async def on_member_join(member):
    channel = member.guild.text_channels[0]
    await channel.send('Welcome!')
© www.soinside.com 2019 - 2024. All rights reserved.