简单的 bot 命令在 discord.py 中不起作用
Simple bot command is not working in discord.py
我想知道管理员要在哪些文本频道中启用我的机器人功能。但在这种情况下,我的代码无法正常工作。
主要思想是当管理员在文本聊天中输入 !enable
时,bot 对其做出反应并将文本聊天 id、公会 id(ctx.channel.id
) 添加到列表中,然后 bot 响应与 bot has been enabled
.
聊天
此命令不起作用的代码:
channel = []
@bot.command()
async def enable(ctx):
global channel
print("Debug")
await ctx.send('Restriction bot has been enabled for this text channel.')
channel.append(ctx.channel.id)
完整的机器人代码:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
@bot.event
async def on_ready():
print(f'We have logged in as {bot.user.name}.')
channel = []
@bot.command()
async def enable(ctx):
global channel
print("Debug")
await ctx.send('Restriction bot has been enabled for this text channel.')
channel.append(ctx.channel.id)
@bot.event
async def on_message(ctx):
if ctx.author == bot.user:
return
#if ctx.channel.id != [for channnels in channel]:
# return
if ctx.attachments[0].height:
await ctx.author.send('Your media file is restricted!')
await ctx.delete()
channel
变量已在您的程序中初始化,因此每次您重新启动机器人时,它都会被清空。解决问题的一种方法是将它们存储在文件中。最简单的方法是使用 json 库。您需要创建一个 channels.json
文件。
- 代码:
from json import loads, dumps
def get_data():
with open('channels.json', 'r') as file:
return loads(file.read())
def set_data(chan):
with open('channels.json', 'w') as file:
file.write(dumps(chan, indent=2))
@bot.command()
async def enable(ctx):
channels = get_data()
channels.append(ctx.channel.id)
set_data(channels)
await ctx.send('Restriction bot has been enabled for this text channel.')
channels.json
文件:
[]
我想知道管理员要在哪些文本频道中启用我的机器人功能。但在这种情况下,我的代码无法正常工作。
主要思想是当管理员在文本聊天中输入 !enable
时,bot 对其做出反应并将文本聊天 id、公会 id(ctx.channel.id
) 添加到列表中,然后 bot 响应与 bot has been enabled
.
此命令不起作用的代码:
channel = []
@bot.command()
async def enable(ctx):
global channel
print("Debug")
await ctx.send('Restriction bot has been enabled for this text channel.')
channel.append(ctx.channel.id)
完整的机器人代码:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
@bot.event
async def on_ready():
print(f'We have logged in as {bot.user.name}.')
channel = []
@bot.command()
async def enable(ctx):
global channel
print("Debug")
await ctx.send('Restriction bot has been enabled for this text channel.')
channel.append(ctx.channel.id)
@bot.event
async def on_message(ctx):
if ctx.author == bot.user:
return
#if ctx.channel.id != [for channnels in channel]:
# return
if ctx.attachments[0].height:
await ctx.author.send('Your media file is restricted!')
await ctx.delete()
channel
变量已在您的程序中初始化,因此每次您重新启动机器人时,它都会被清空。解决问题的一种方法是将它们存储在文件中。最简单的方法是使用 json 库。您需要创建一个 channels.json
文件。
- 代码:
from json import loads, dumps
def get_data():
with open('channels.json', 'r') as file:
return loads(file.read())
def set_data(chan):
with open('channels.json', 'w') as file:
file.write(dumps(chan, indent=2))
@bot.command()
async def enable(ctx):
channels = get_data()
channels.append(ctx.channel.id)
set_data(channels)
await ctx.send('Restriction bot has been enabled for this text channel.')
channels.json
文件:
[]