Skip to content

Instantly share code, notes, and snippets.

@acelan
Created July 27, 2018 13:58
Show Gist options
  • Save acelan/b22023dcc9dbad2523c6656f2df13a55 to your computer and use it in GitHub Desktop.
Save acelan/b22023dcc9dbad2523c6656f2df13a55 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import discord
import asyncio
import sys
import random
import feedparser
import ssl
import re
import valve.rcon
import os
import time
valve.rcon.RCONMessage.ENCODING = "utf-8"
token = os.getenv("CONANEXILES_Game_DiscordPlugin_Token")
channel_id = os.getenv("CONANEXILES_Game_DiscordPlugin_Channel")
address = ("localhost", 25575)
password = os.getenv("CONANEXILES_Game_RconPlugin_RconPassword")
client = discord.Client()
feedparser.PREFERRED_XML_PARSERS.remove('drv_libxml2')
def rcon_send_msg(msg):
with valve.rcon.RCON(address, password) as rcon:
response = rcon.execute("server %s" % msg)
#print(response.text)
print('To game: server %s' % msg)
async def read_game_chat():
async def follow(thefile):
thefile.seek(0,2)
while True:
line = thefile.readline()
if not line:
await asyncio.sleep(1)
continue
if "ChatWindow" in line:
yield line
await client.wait_until_ready()
logfile = open("/conanexiles/ConanSandbox/Saved/Logs/ConanSandbox.log","r", encoding="utf-8")
loglines = follow(logfile)
channel = discord.Object(id=channel_id)
async for line in loglines:
str = re.sub(r' said', '', re.sub(r'^.*Character ', '', line))
print("From game: " + str)
if str.split(':')[1].startswith(' !'):
continue
await client.send_message(channel, str)
await asyncio.sleep(1)
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
@client.event
async def on_message(message):
if message.author == client.user:
return
else: # send into game
rcon_send_msg(message.author.name + ": " + message.content)
client.loop.create_task(read_game_chat())
client.run(token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment