discord.py(重写)如何将命令使用能力缩小到单个通道?
discord.py (rewrite) How can I narrow a commands use ability to a single channel?
我正在尝试使此代码仅在特定频道中有效。当我尝试在正确的通道中执行命令时,它只会发送大量错误。如果你想测试它,我会添加我的导入。上次我试图问这个问题,但被拒绝了。我仍然不知道该怎么做,只需要一点帮助,因为我是编写 discord 机器人的新手。
import discord
from discord.ext import commands, tasks
import os
import random
import asyncio
from asyncio import gather
client = commands.Bot(command_prefix='.')
@client.command()
async def car(ctx):
pictures = [
'https://car-images.bauersecure.com/pagefiles/78294/la_auto_show_11.jpg',
'http://www.azstreetcustom.com/uploads/2/7/8/9/2789892/az-street-custom-gt40-2_orig.jpg',
'http://tenwheel.com/imgs/a/b/l/t/z/1967_firebird_1968_69_70_2000_camaro_blended_custom_supercharged_street_car_1_lgw.jpg',
'https://rthirtytwotaka.files.wordpress.com/2013/06/dsc_0019.jpg',
'http://speedhunters-wp-production.s3.amazonaws.com/wp-content/uploads/2008/06/fluke27.jpg',
'https://i.ytimg.com/vi/pCt0KXC1tng/maxresdefault.jpg',
'https://i2.wp.com/www.tunedinternational.com/featurecars/dorift/02.jpg',
'http://i.imgur.com/nEbyV82.jpg',
'https://cdn.hiconsumption.com/wp-content/uploads/2019/02/Affordable-Vintage-Japanese-Cars-0-Hero-1087x725.jpg',
'http://speedhunters-wp-production.s3.amazonaws.com/wp-content/uploads/2012/04/IMG_0268.jpg',
'https://i.ytimg.com/vi/Y-moGXK2zLk/maxresdefault.jpg',
'https://www.topgear.com/sites/default/files/images/big-read/carousel/2016/03/568cd4ab437c6557c583a6f4a4feb6d1/3carguyscarouselmarch2016.jpg'
]
channel = discord.utils.get()
if channel == 705161333972140072:
await ctx.channel.purge(limit=1)
await ctx.send(f'{random.choice(pictures)}')
client.run('token')
您需要对命令功能进行一些更改:
- 修正缩进
- 使用
ctx.channel.id
代替channel
和discord.utils.get()
- 不清除,而是删除命令消息
@client.command()
async def car(ctx):
pictures = [
'https://car-images.bauersecure.com/pagefiles/78294/la_auto_show_11.jpg',
'http://www.azstreetcustom.com/uploads/2/7/8/9/2789892/az-street-custom-gt40-2_orig.jpg',
'http://tenwheel.com/imgs/a/b/l/t/z/1967_firebird_1968_69_70_2000_camaro_blended_custom_supercharged_street_car_1_lgw.jpg',
'https://rthirtytwotaka.files.wordpress.com/2013/06/dsc_0019.jpg',
'http://speedhunters-wp-production.s3.amazonaws.com/wp-content/uploads/2008/06/fluke27.jpg',
'https://i.ytimg.com/vi/pCt0KXC1tng/maxresdefault.jpg',
'https://i2.wp.com/www.tunedinternational.com/featurecars/dorift/02.jpg',
'http://i.imgur.com/nEbyV82.jpg',
'https://cdn.hiconsumption.com/wp-content/uploads/2019/02/Affordable-Vintage-Japanese-Cars-0-Hero-1087x725.jpg',
'http://speedhunters-wp-production.s3.amazonaws.com/wp-content/uploads/2012/04/IMG_0268.jpg',
'https://i.ytimg.com/vi/Y-moGXK2zLk/maxresdefault.jpg',
'https://www.topgear.com/sites/default/files/images/big-read/carousel/2016/03/568cd4ab437c6557c583a6f4a4feb6d1/3carguyscarouselmarch2016.jpg'
]
if ctx.channel.id == 705161333972140072:
await ctx.message.delete()
await ctx.send(random.choice(pictures))
client.run('token')
您也可以使用checks
我正在尝试使此代码仅在特定频道中有效。当我尝试在正确的通道中执行命令时,它只会发送大量错误。如果你想测试它,我会添加我的导入。上次我试图问这个问题,但被拒绝了。我仍然不知道该怎么做,只需要一点帮助,因为我是编写 discord 机器人的新手。
import discord
from discord.ext import commands, tasks
import os
import random
import asyncio
from asyncio import gather
client = commands.Bot(command_prefix='.')
@client.command()
async def car(ctx):
pictures = [
'https://car-images.bauersecure.com/pagefiles/78294/la_auto_show_11.jpg',
'http://www.azstreetcustom.com/uploads/2/7/8/9/2789892/az-street-custom-gt40-2_orig.jpg',
'http://tenwheel.com/imgs/a/b/l/t/z/1967_firebird_1968_69_70_2000_camaro_blended_custom_supercharged_street_car_1_lgw.jpg',
'https://rthirtytwotaka.files.wordpress.com/2013/06/dsc_0019.jpg',
'http://speedhunters-wp-production.s3.amazonaws.com/wp-content/uploads/2008/06/fluke27.jpg',
'https://i.ytimg.com/vi/pCt0KXC1tng/maxresdefault.jpg',
'https://i2.wp.com/www.tunedinternational.com/featurecars/dorift/02.jpg',
'http://i.imgur.com/nEbyV82.jpg',
'https://cdn.hiconsumption.com/wp-content/uploads/2019/02/Affordable-Vintage-Japanese-Cars-0-Hero-1087x725.jpg',
'http://speedhunters-wp-production.s3.amazonaws.com/wp-content/uploads/2012/04/IMG_0268.jpg',
'https://i.ytimg.com/vi/Y-moGXK2zLk/maxresdefault.jpg',
'https://www.topgear.com/sites/default/files/images/big-read/carousel/2016/03/568cd4ab437c6557c583a6f4a4feb6d1/3carguyscarouselmarch2016.jpg'
]
channel = discord.utils.get()
if channel == 705161333972140072:
await ctx.channel.purge(limit=1)
await ctx.send(f'{random.choice(pictures)}')
client.run('token')
您需要对命令功能进行一些更改:
- 修正缩进
- 使用
ctx.channel.id
代替channel
和discord.utils.get()
- 不清除,而是删除命令消息
@client.command()
async def car(ctx):
pictures = [
'https://car-images.bauersecure.com/pagefiles/78294/la_auto_show_11.jpg',
'http://www.azstreetcustom.com/uploads/2/7/8/9/2789892/az-street-custom-gt40-2_orig.jpg',
'http://tenwheel.com/imgs/a/b/l/t/z/1967_firebird_1968_69_70_2000_camaro_blended_custom_supercharged_street_car_1_lgw.jpg',
'https://rthirtytwotaka.files.wordpress.com/2013/06/dsc_0019.jpg',
'http://speedhunters-wp-production.s3.amazonaws.com/wp-content/uploads/2008/06/fluke27.jpg',
'https://i.ytimg.com/vi/pCt0KXC1tng/maxresdefault.jpg',
'https://i2.wp.com/www.tunedinternational.com/featurecars/dorift/02.jpg',
'http://i.imgur.com/nEbyV82.jpg',
'https://cdn.hiconsumption.com/wp-content/uploads/2019/02/Affordable-Vintage-Japanese-Cars-0-Hero-1087x725.jpg',
'http://speedhunters-wp-production.s3.amazonaws.com/wp-content/uploads/2012/04/IMG_0268.jpg',
'https://i.ytimg.com/vi/Y-moGXK2zLk/maxresdefault.jpg',
'https://www.topgear.com/sites/default/files/images/big-read/carousel/2016/03/568cd4ab437c6557c583a6f4a4feb6d1/3carguyscarouselmarch2016.jpg'
]
if ctx.channel.id == 705161333972140072:
await ctx.message.delete()
await ctx.send(random.choice(pictures))
client.run('token')
您也可以使用checks