Skip to content

Instantly share code, notes, and snippets.

@oosawa
Last active February 28, 2018 14:22
Show Gist options
  • Save oosawa/9d8524d02e58b463cf66567dab46182b to your computer and use it in GitHub Desktop.
Save oosawa/9d8524d02e58b463cf66567dab46182b to your computer and use it in GitHub Desktop.
マルコフ連鎖生成プログラムを使ったDiscordへの無差別投稿用Bot
#coding:utf-8
# ディレクトリの中にInuフォルダを作って、MarkovInu.exe などを放り込んで動かします
# Windows10 + Python3.6.4
import discord
import os
import subprocess
from subprocess import Popen
from time import sleep
import random
client = discord.Client()
base_dir = os.path.dirname(os.path.abspath(__file__))
cmd = os.path.dirname(os.path.abspath(__file__)) + r'\Inu\MarkovInu.exe > murmur.txt'
print(os.path.dirname(cmd))
os.chdir(os.path.dirname(cmd))
@client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == client.user:
return
if message.channel.name != 'maou-no-niwa':
return
# 特定のチャンネルで発言があったら、その発言を学習用データにし、マルコフ連鎖で生成したテキストを投稿する
# 学習用データに追記。いちおうチャンネルごと
f = open('messages_' + message.channel.id + '.txt','a', encoding="utf-8")
f.write(message.content)
f.write('\n')
f.close()
# マルコフ連鎖で文章生成する
proc = Popen(cmd,shell=True )
sleep(5 * random.random()) # あんまりはやく返事するとBotっぽいのでちょっと待つ
proc.terminate()
f = open(base_dir + r'\Inu\murmur.txt', 'r')
murmur = f.read() # readモードで全部読み込む
f.close()
murmur = murmur.strip()
msg = (murmur).format(message)
await client.send_message(message.channel, msg)
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
client.run('トークンを入れてね')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment