简体   繁体   English

如何让我的机器人从用户运行命令的位置获取通道 ID? (不和谐.py)

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

So, I have made a bot that fetches memes from reddit and posts those memes to a Discord Channel.因此,我制作了一个机器人,可以从reddit中获取meme ,并将这些 meme 发布到Discord频道。

The only reason the word 'Discord Channel' is not plural , is because at the moment I can only input the channel I want to send those memes to manually , by inserting the ID into my code.. “Discord Channel”这个词不是复数的唯一原因是因为目前我只能通过在我的代码中插入ID 来手动输入我想要发送这些模因的频道。

A very big problem when it comes to releasing my bot to the public.在向公众发布我的机器人时,这是一个非常大的问题。 This is all I have so far.这就是我到目前为止所拥有的。

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')

Please tell me if I need to import a few things to make this happen!请告诉我是否需要导入一些东西来实现这一点! Also, I know this question can be answered by reading the Discord Py API but I don't know how to read the API.另外,我知道这个问题可以通过阅读 Discord Py API 来回答,但我不知道如何阅读 API。

You can get the channel object with ctx.channel .您可以使用ctx.channel获取通道对象。 Then you can get the id with ctx.channel.id .然后您可以使用ctx.channel.id获取 id。 So you can do:所以你可以这样做:

@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