简体   繁体   English

如何使 discord.py bot 发送到它被调用的服务器?

[英]How to make discord.py bot send to the server it's called in?

had a question regarding discord.py.有一个关于 discord.py 的问题。 I run two separate servers that my bot is on, my test server and my main server.我运行我的机器人所在的两个单独的服务器,我的测试服务器和我的主服务器。 The problem is when I send a message in the test server, the bot then sends its message to the main server, and never sends it back to the server the command is being called in (only in functions).问题是当我在测试服务器中发送消息时,机器人会将其消息发送到主服务器,并且永远不会将其发送回正在调用命令的服务器(仅在函数中)。

For example:例如:

 if message.content == '!Hello':
 await message.channel.send('Hello there!')

If I type the above in the test server, my bot will respond with "Hello there."如果我在测试服务器中输入上述内容,我的机器人将回复“你好”。 in the test server like it should, However: if I tried putting this code in a function and called it:在测试服务器中应该这样,但是:如果我尝试将此代码放入 function 并调用它:

if message.content == "!Hello":
    await hellomessage()

async def hellomessage():
    channel = client.get_channel('Channel ID Here')
    await channel.send('Hello there!')

The channel ID is obviously set to a specific server.频道 ID 显然设置为特定的服务器。 So say I have ID '1234' as my main server and ID '1111' as my test server, regardless whether I call it in my test server or main server, that message is going to send to the main server because the ID is no different.所以说我有 ID '1234' 作为我的主服务器和 ID '1111' 作为我的测试服务器,无论我是在我的测试服务器还是主服务器中调用它,该消息都会发送到主服务器,因为 ID 是 no不同的。 My question is how do I make sure the "channel" attribute changes depending on the server it was called in. I want it so if I say,Hello in my test server.我的问题是如何确保“通道”属性根据调用它的服务器而变化。我想要它,所以如果我在我的测试服务器中说,你好。 it doesn't send to the main server and only the test server.它不发送到主服务器,只发送到测试服务器。

Seems like a very trivial answer but I'm just struggling to find it, any help is appreciated!似乎是一个非常微不足道的答案,但我只是在努力寻找它,感谢任何帮助!

You can check what guild a message has been sent from using the .guild attribute of a message.您可以使用消息的.guild属性检查消息是从哪个公会发送的。

Example:例子:

# You can also have a separate coroutine for your main server
async def hello_test_message():
    test_guild_channel = client.get_channel(ID_HERE)
    await test_guild_channel.send("Hello there!")

@client.event
async def on_message(message):
    if client.user == message.author:
        return

    if message.guild.id == TEST_GUILD_ID_HERE:
        if message.content.lower() == "!hello":  # .lower() for case insensitivity
            await hello_test_message()
    # Another condition for your main server + calling the other coroutine

This being said, I'm assuming that you're not hard-coding values in values for every possible channel etc. and if that is the case, and you just want the bot to respond in the original message's channel, you can use message.channel.send(... .话虽这么说,我假设您没有在每个可能的通道等的值中硬编码值,如果是这种情况,并且您只希望机器人在原始消息的通道中响应,您可以使用message.channel.send(...

The current method of doing things will result in quite a bit of duplicate code.当前的做事方法会导致相当多的重复代码。

I'd also recommend having a look into the commands extension of discord instead of using on_message events for them.我还建议查看 discord 的命令扩展,而不是为它们使用on_message事件。


References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM