簡體   English   中英

如何讓我的機器人從用戶運行命令的位置獲取通道 ID? (不和諧.py)

[英]How do I make my bot get the channel Id from where the user ran the command from? (Discord.py)

因此,我制作了一個機器人,可以從reddit中獲取meme ,並將這些 meme 發布到Discord頻道。

“Discord Channel”這個詞不是復數的唯一原因是因為目前我只能通過在我的代碼中插入ID 來手動輸入我想要發送這些模因的頻道。

在向公眾發布我的機器人時,這是一個非常大的問題。 這就是我到目前為止所擁有的。

import discord
from discord.ext import commands
import asyncio

@client.command()
async def config(ctx):
    await ctx.send('Hello, please go to the channel, you would like me to post memes to, and do am.memechannel')
    
    
@client.command()
async def memechannel(ctx):
    await ctx.send('Memes will now be sent in this channel')

請告訴我是否需要導入一些東西來實現這一點! 另外,我知道這個問題可以通過閱讀 Discord Py API 來回答,但我不知道如何閱讀 API。

您可以使用ctx.channel獲取通道對象。 然后您可以使用ctx.channel.id獲取 id。 所以你可以這樣做:

@client.command()
async def memechannel(ctx):
    channel_id = ctx.channel.id
    await ctx.send('Memes will now be sent in this channel')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM