因此,我正在构建这个不和谐的机器人,它从“我的世界”服务器读取latest.log文件,并通过特定的通道发送用户、前进和死亡消息。现在的问题是,过了一段时间,机器人开始发送多条消息,我真的不知道为什么。我尝试添加我已经在文件中发送的行,并交叉检查新行以查看它们是否重复。这大约是我的朋友(在他的电脑上运行我的机器人)向我发送他的python.exe窗口的屏幕截图的时候,其中多次显示“机器人在线”的消息。我最好的猜测是多个机器人实例同时运行,但我不知道它是如何做到这一点的。有人能帮帮我吗?这是我的代码:
from discord.ext import commands
client = commands.Bot(command_prefix='.')
deth_msgs = ['was shot', 'was pummeled by', 'was pricked to death',
'walked into a cactus whilst trying to escape', 'drowned', 'experienced kinetic energy whilst trying to escape', 'blew up', 'was blown up by',
'was killed by', 'hit the ground too hard', 'hit the ground too hard whilst trying to escape',
'fell from a high place', 'fell off', 'was impaled', 'was squashed by', 'was skewered by', 'went up in flames', 'walked into fire whilst fighting',
'burned to death',
'was burnt to a crisp whilst fighting', 'went off with a bang', 'tried to swim in lava', 'was struck by lightning', 'discovered the floor was lava',
'walked into danger zone due to', 'was killed', 'froze to death', 'was frozen to death by', 'was slain by', 'was fireballed by', 'was stung to death',
'was shot by a skull from', 'starved to death', 'suffocated in a wall', 'was squished too much', 'was poked to death', 'was impaled by',
'fell out of the world', "didn't want to live in the same world as"]
players = []
with open('usercache.json') as u_file:
details = json.load(u_file)
for dict in details:
players.append(dict['name'])
try:
f = open("dupli_lines.txt")
except FileNotFoundError:
f = open('dupli_lines.txt', 'w+')
finally:
f_stats = os.stat('dupli_lines.txt')
f.close()
if f_stats.st_size > 1000000:
os.remove('dupli_lines.txt')
f = open('dupli_lines.txt', 'w+')
f.close()
#------------------------------------------------ Events -----------------------------------------------------------------------
@client.event
async def on_ready():
print('Bot is Online')
channel = client.get_channel(id)
pos = 0
while True:
with open('dupli_lines.txt','r') as f:
logs = f.read()
with open(absolutepath) as file:
file.seek(pos)
line = file.readline()
if line not in logs:
# if 'joined the game\n' in line:
# string = line[33:]
# player = string.replace(' joined the game\n', '')
# if check_dup(player) == False:
# players.append(player)
if '!coords' in line:
if line not in logs:
with open('dupli_lines.txt', 'a+') as f:
if line not in logs:
f.write(line)
string = line[33:].replace('!coords', '')
words = string.split()
for player in players:
for word in words:
if player in word:
embed = discord.Embed(
title=player,
colour=discord.Colour.from_rgb(57, 255, 20)
)
words.pop(0)
tup_words = tuple(words[1:])
msg = ' '.join(tup_words)
embed.add_field(
name=words[0], value=msg, inline = False)
embed.set_thumbnail(
url='http://static.wikia.nocookie.net/minecraft_gamepedia/images/2/2c/Compass_JE3.gif/revision/latest/scale-to-width-down/150?cb=20201125191224')
await channel.send(embed=embed)
break
time.sleep(1)
break
if line[33:].startswith('<'):
await channel.send(line[33:])
with open('dupli_lines.txt', 'a+') as f:
f.write(line)
time.sleep(1)
if 'made the advancement' in line:
embed = discord.Embed(
title = line[33:],
colour=discord.Colour.from_rgb(255,215,0)
)
embed.set_thumbnail(
url='https://www.pikpng.com/pngl/b/456-4569150_michael-rosen-noice-png-brian-rosen-clipart.png')
await channel.send(embed=embed)
with open('dupli_lines.txt', 'a+') as f:
f.write(line)
time.sleep(1)
else:
for msg in deth_msgs:
if msg in line:
await channel.send(line[33:])
with open('dupli_lines.txt', 'a+') as f:
f.write(line)
break
time.sleep(1)
pos = file.tell()
client.run('TOKEN')这就是python窗口中显示的内容:Bot is Online x 6
附言:我知道我所有的代码都是在on_ready()函数中编写的,但我打算使用这个机器人就是为了这个特殊的原因,i.e.to读取游戏中的消息并将它们呈现在不一致通道中。
发布于 2021-05-23 21:40:06
如果多个实例正在运行,则始终可以更改bot令牌,以撤销任何正在运行的实例的访问权限。
您可以通过转到https://discord.com/developers/applications来做到这一点
https://stackoverflow.com/questions/67153218
复制相似问题